Sidebar

How do I add a JSON array to a JSON array from an HL7 2.3.1 message?

0 votes
629 views
asked Oct 3, 2018 by michael-h-5027 (14,350 points)
I would like to use the sample message below and create a for loop of ORC segments and then another for loop inside of each ORC group for each DG1 segment.

MSH|^~\&|LinkLogic-1000-39871-12303|TEST000^PC|SOARCLAB|PC|20170913142300||ORM^O01|1820931774473800|P|2.3.1|||NE|NE
EVN|O01|20170913142300
PID|1|0|12345||Clinical^Ann^Patient||19350927|F||2028-9|43 Happy Lane^^Glasgow^KY^42141||^^^ann.p.clinical@yahoo.com~(270) 555-9966^^CP~(270) 555-4532^^BP|(270) 555-2340|English|M|||111-11-1112|||D||||Y||||
PV1|1|O|^^^PC||||BMallory
ORC|NW|655422-1|||||||20170913|||rclouse
OBR|1|655422-1||CPT-78300^Bone Scan Limited|||20170913||1||N|||||rclouse|||||||||||^^^^^R
DG1|1||Z85.41^CERVICAL CANCER^I10
DG1|2||I38^VALVULAR HEART DISEASE^I10
ORC|NW|655422-2|||||||20170914|||rclouse
OBR|1|655422-2||CPT-77051^CT Abdomen|||20170914||1||N|||||rclouse|||||||||||^^^^^R
NTE|1||What are you trying to rule out? Test
NTE|2||Should you rule out?
DG1|1||C71.9^BRAIN TUMOR^I10
DG1|2||R00.2^FLUTTERING HEART^I10
ORC|NW|655422-3|||||||20170915|||rclouse
OBR|1|655422-3||3076718^Bilirubin Total_BILIT|||20170915||1||N|||||rclouse|||||||||||^^^^^R
NTE|1||CPT-82247  g
DG1|1||Z85.41^CERVICAL CANCER^I10
DG1|2||I38^VALVULAR HEART DISEASE^I10
DG1|3||C71.9^BRAIN TUMOR^I10
DG1|4||R00.2^FLUTTERING HEART^I10

1 Answer

0 votes
// 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);
answered Oct 3, 2018 by michael-h-5027 (14,350 points)
...