Last active
May 13, 2021 18:56
-
-
Save shadowdoc/5884834 to your computer and use it in GitHub Desktop.
Mirth Connect transformer to create json from E4X XML also including the original message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// One way converter from E4X XML to JSON | |
// 1) turns <body><item>1</item><item>2</item></body> into | |
// body: {item: ["1", "2"]} so that lists are easier to work with | |
// 2) turns things like: <body a="a">whatever</body> into | |
// body: {_a: "a", _: "whatever"} | |
// however <body>whatever</body> becomes simply body: "whatever | |
// - attributes specified by ignored are ignored | |
function E4XtoJSON(xml, ignored) { | |
var r, children = xml.*, attributes = xml.@*, length = children.length(); | |
if(length == 0) { | |
r = xml.toString(); | |
} else if(length == 1) { | |
var text = xml.text().toString(); | |
if(text) { | |
r = text; | |
} | |
} | |
if(r == undefined) { | |
r = {}; | |
for each (var child in children) { | |
var name = child.localName(); | |
var json = E4XtoJSON(child, ignored); | |
var value = r[name]; | |
if(value) { | |
if(value.length) { | |
value.push(json); | |
} else { | |
r[name] = [value, json] | |
} | |
} else { | |
r[name] = json; | |
} | |
} | |
} | |
if(attributes.length()) { | |
var a = {}, c = 0; | |
for each (var attribute in attributes) { | |
var name = attribute.localName(); | |
if(ignored && ignored.indexOf(name) == -1) { | |
a["_" + name] = attribute.toString(); | |
c ++; | |
} | |
} | |
if(c) { | |
if(r) a._ = r; | |
return a; | |
} | |
} | |
return r; | |
} | |
var json = ''; | |
var new_msg = msg; // XML version of the message | |
var original = messageObject.getRawData(); // Raw HL7 message | |
new_msg.original_message = original.toString(); | |
json = JSON.stringify(E4XtoJSON(new_msg), null, ' '); // Convert XML HL7 to json | |
// json = JSON.parse(json); // turn it back into a javascript object | |
// json.orignal_message = original.toString(); | |
// json = JSON.stringify(json); | |
channelMap.put('xml', msg); | |
channelMap.put('json', json); |
Howdy Marc, not sure if you check this anymore but if was want to assign a string of elements to ignore, ie:
var inValidKeys = ['REF02','REF02','NM103','NM104','NM105','PER03','PER04','PER05','PER06','PER07','N301','N302','N401','N402 ','N403 ','N406','DMG02','DMG03','LUI01','LUI02','HD04','HD05','DTP03','NM103','NM104','NM109'];
How would I get this to work with the function?
Hey AnimalMind - I do still use github, but haven't used Mirth for quite some time. Apologies, I can't help!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Sir,
I have used your code in mirth connect , it works fine for 80 % messages , but for few xml messages like
"AdditionalMessageInformation": [
"MEMBER MUST USE MAIL ORDER.",
"PLAN LIMITATIONS EXCEEDED"
],
If any node is having multiple values for same xml element or xml element is repeating, code fails there.
For example in xml
01MEMBER MUST USE MAIL ORDER.02PLAN LIMITATIONS EXCEEDED
"AdditionalMessageInformation" is two times so code fails there.
Please help here i am stucked and not that much good in code
Appreciate your help.