web stats
Help Needed For Dynamic JSON Creation. - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 07-03-2019, 02:29 AM
asaxena asaxena is offline
Mirth Newb
 
Join Date: Jun 2017
Posts: 15
asaxena is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 07-03-2019, 06:52 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 997
agermano is on a distinguished road
Default

What mirth version?
Reply With Quote
  #3  
Old 07-04-2019, 04:47 AM
aTom aTom is offline
OBX.3 Kenobi
 
Join Date: Feb 2016
Location: Lake Geneva Area
Posts: 113
aTom is on a distinguished road
Default

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

Code:
// 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()]];
		}
		tmp['PID'][field.localName()].push(jsonField);
	}
	
}
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 (https://www.mirthcorp.com/community/...2&postcount=22) on your hl7 message before applying this code so that all empty fields/components will be removed.
__________________
Tom

Last edited by aTom; 07-04-2019 at 06:21 AM. Reason: some comments added
Reply With Quote
  #4  
Old 07-04-2019, 11:51 AM
asaxena asaxena is offline
Mirth Newb
 
Join Date: Jun 2017
Posts: 15
asaxena is on a distinguished road
Default

@agermano I am using 3.6.2 version of Mirth connect.
Reply With Quote
  #5  
Old 07-04-2019, 11:53 AM
asaxena asaxena is offline
Mirth Newb
 
Join Date: Jun 2017
Posts: 15
asaxena is on a distinguished road
Default

@aTom Thanks for the reply and guidance, I will use your method and if it will resolve my problem I will let you know.
Reply With Quote
Reply

Tags
dynamic coding, hl7tojson, json, support

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 12:27 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation