// Create new JSON message
message = qie.createJSONMessage(null, 'UTF-8');
// Add 'nodes' to the message
var patientId = source.getNode("PID-3.1");
message.setJSONString('/id', patientId);
var dob = source.getNode("PID-7");
message.setJSONNumber("/dob", dob);
var lastName = source.getNode("PID-5.1");
message.setJSONString("/lastName", lastName);
var citizenship = source.getNode("PID-26");
if (StringUtils.equalsIgnoreCase(citizenship, 'Y')) {
message.setJSONBoolean('/citizenship', true);
} else {
message.setJSONBoolean('/citizenship', false);
}
// Only do this logic if the ORC segment exists
if (source.checkNodeExists('ORC')) {
//Create the JSON array '/orcSegments' using set.JSONArray
message.setJSONArray('/orcSegments');
// Loop through the ORC segments to get the detail
var orcGroups = source.getAllNodes('ORC[group=!ORC]');
for (var i = 0; i < orcGroups.length; i++) {
var orcGroup = qie.parseHL7String(orcGroups[i]);
// Add an empty object to the array. '{}' denotes an empty object.
message.addObjectToJSONArray('/orcSegments', qie.parseJSONString('{}', 'UTF-8'));
// Loop through the orc segments and add the data to the object /orcSegments.
message.setJSONString('/orcSegments/['+(i+1)+']/id', orcGroup.getNode('ORC-2'));
message.setJSONString('/orcSegments/['+(i+1)+']/cpt', orcGroup.getNode('OBR-4.1'));
message.setJSONString('/orcSegments/['+(i+1)+']/cptDescription', orcGroup.getNode('OBR-4.2'));
// Loop through the nte segments and add to the /orcSegments object.
var nteGroups = orcGroup.getAllNodes('NTE[group=!NTE]');
for (var k = 0; k < nteGroups.length; k++) {
var nteGroup = qie.parseHL7String(nteGroups[k]);
message.setJSONString('/orcSegments/['+(i+1)+']/notes', message.getNode('/orcSegments/['+(i+1)+']/notes') + ' ' + nteGroup.getNode('NTE-3'));
}
// Add the array /dg1Segments to the /orcSegments object.
message.setJSONArray('/orcSegments/['+(i+1)+']/dg1Segments');
var dg1Groups = orcGroup.getAllNodes('DG1[group=!DG1]');
for (var l = 0; l < dg1Groups.length; l++) {
var dg1Group = qie.parseHL7String(dg1Groups[l]);
// Add another empty object '/dg1Segments' to the array.
message.addObjectToJSONArray('/orcSegments/['+(i+1)+']/dg1Segments', qie.parseJSONString('{}', 'UTF-8'));
// Now you can add the data to the newly created object.
message.setJSONString('/orcSegments/['+(i+1)+']/dg1Segments/['+(l+1)+']/codeValue', dg1Group.getNode('DG1-3.1'));
message.setJSONString('/orcSegments/['+(i+1)+']/dg1Segments/['+(l+1)+']/description', dg1Group.getNode('DG1-3.2'));
message.setJSONString('/orcSegments/['+(i+1)+']/dg1Segments/['+(l+1)+']/coding', dg1Group.getNode('DG1-3.3'));
}
}
}
// Last, format the JSON message with the desired indent level.
// This is for readibility, it's not required.
message.formatJSON(2);