No announcement yet.

Help Needed For Dynamic JSON Creation.

  • Filter
  • Time
  • Show
Clear All
new posts

  • Help Needed For Dynamic JSON Creation.

    Hi All,

    I am facing one challenge while converting HL7 message into JSON.
    I want to create JSON key:value pair dynamically. For this project we are focusing on MSH, PID, PV1 and DG1 segments only and we want to create a very robust interface.
    We want to create JSON node only if the corresponding field is present in HL7 message,E.g. if PID.5 Field is present in HL7 only then JSON will create with Patient Name node. Problem I am facing here is that PID segment has 30 fields and PV1 has 52 fields, I don't want to put so many If else condition and create JSON after checking fields length inside the code. Can someone suggest me a better approach for this.
    I hope I was able to explain the scenario properly here. if additional clarification needed please let me know in comment, I will try to explain it in a better way.

    Thanks in advance.

  • #2
    What mirth version?


    • #3
      If you just want to convert from HL7 to json, here would be the basic idea :

      // assuming msg is your incoming HL7 message and tmp your json template
      tmp['PID'] = {};
      for each(var field in msg['PID'].children()) {	
      	// build field with its components
      	var jsonField = {};
      	for each(var component in field.children()) { // browse its children : PID.x.1, PID.x.2, ...
      		if (typeof jsonField[component.localName()] == "undefined") { // not seen yet
      			jsonField[component.localName()] = component.toString();			
      		} else {
      			if(!Array.isArray(jsonField[component.localName()])) { // repeating component ? not sure it is actually possible...
      				jsonField[component.localName()] = [jsonField[component.localName()]] ; // convert to array			
      			jsonField[component.localName()].push(component.toString()); // insert 
          // now insert this field into the JSON segment
      	if(typeof tmp['PID'][field.localName()] == "undefined") {
      		tmp['PID'][field.localName()] = jsonField;
      	} else {
      		if (!Array.isArray(tmp['PID'][field.localName()])) { // repeating field (~) ?
      			tmp['PID'][field.localName()] = [tmp['PID'][field.localName()]];
      This may be a good start, you'll have to handle repeating segments (e.g. DG1) the same way as repeating fields in this code sample, and you would also need to refine this if you parse subcomponents, probably using a more recursive approach.

      Finally you might apply stripEmptyNodes script ( on your hl7 message before applying this code so that all empty fields/components will be removed.
      Last edited by aTom; 07-04-2019, 05:21 AM. Reason: some comments added


      • #4
        @agermano I am using 3.6.2 version of Mirth connect.


        • #5
          @aTom Thanks for the reply and guidance, I will use your method and if it will resolve my problem I will let you know.