web stats
Need help parsing XML using Javascript - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 10-22-2019, 10:25 AM
tjoyner1 tjoyner1 is offline
Mirth Newb
 
Join Date: Sep 2019
Posts: 10
tjoyner1 is on a distinguished road
Question Need help parsing XML using Javascript

Using Mirth Connect 3.7.1. I have channel that uses destinations to:

1 destination - HTTP Sender to use a POST call to receive an authorization token. Put the token into a variable to use in the next destination.

2 destination - HTTP Sender to use a GET call to receive an HTTP response in XML format.

3 destination - SQL writer uses the #2 destinations response, parses, maps parsed data, writes to a database.

I am having issues with 3destination... or could be between 2 & 3.... also, make note, I am new to all coding. But Mirth is JavaScript friendly, so that is what is going to be used here. All standard, no custom libraries or anything of the sort.

I receive the the response from 2destination and all the data I need is located in one spot of the XML... ... to make matters worse, there is repeating sets of data each set contained within curly brackets {name, location, credit}, {name, location, credit}, {name, location, credit} and so on...

The above inside the curly brackets is an example... not the actual parts.

I've looked at many forums and I believe it's very possible the answer is out there but being new to JS I could be having newbie issues making the syntax work for me...

I believe I have incorrectly used JSON.parse as well as incorrectly used parser = new DOMParser(); also incorrectly used xmlhttp.responseXML; etc etc...

All in JavaScript GRB is Get Response Body (used as a variable to equal the portion of the XML where my data set is)

/*
***result is DOMParser is not defined***
var GRB = msg['Body'].toString();
var GRBparser = new DOMParser();
var GRBDOM = GRBParser.parseFromString(GRB, "application/xml");
*/

/*
var GRB = msg['Body'].toString();
GRB = GRB.split('').forEach('{}');
*/

/*
var GRB = msg['Body'].toString().split('{}');
for each('{}') = i = 0; i++;
*/
Again, I am new to this and looking for some help

It would be ideal if the data would result in something like this =

nameF : John, nameL : Doe, location : University, date : 10/11/2019,

nameF : Sara, nameL: Conner, location : Pixie Land, date : 10/12/2019,

instead I get the following below ***It's the part at that gives me the problem, all the data is there, I am replacing the actual data with fictitious data because HIPPA=

<HttpResponse><Status>HTTP/1.1 200 OK</Status><Header><Field>
<Name>Server</Name><Value>Microsoft-IIS/10.0</Value></Field><Field>
<Name>X-Content-Type-Options</Name><Value>nosniff</Value></Field><Field>
<Name>Connection</Name><Value>keep-alive</Value></Field><Field>
<Name>Pragma</Name><Value>no-cache</Value></Field><Field><Name>X-
Permitted-Cross-Domain-Policies</Name><Value>none</Value></Field><Field>
<Name>Date</Name><Value>Mon, 14 Oct 2019 18:18:49 GMT</Value></Field>
<Field><Name>Strict-Transport-Security</Name><Value>max-
age=31536000</Value></Field><Field><Name>Cache-Control</Name><Value>no-
cache</Value></Field><Field><Name>Content-Security-Policy</Name>
<Value>default-src https: 'unsafe-inline' 'unsafe-eval'</Value></Field>
<Field><Name>X-AspNet-Version</Name><Value>4.0.30319</Value></Field>
<Field><Name>Vary</Name><Value>Accept-Encoding</Value></Field><Field>
<Name>Expires</Name><Value>-1</Value></Field><Field><Name>Content-
Type</Name><Value>application/json; charset=utf-8</Value></Field>
</Header>
<Body multipart="no">[{"nameF": "John", "nameL": "Doe", "location":
"University", "date": "10/11/2019"}, {"nameF": "Sara", "nameL": "Conner",
"location": "Pixie Land", "date": "10/12/2019"}</Body></HttpResponse
Reply With Quote
  #2  
Old 10-22-2019, 10:44 AM
jackwhaines jackwhaines is offline
 
Join Date: May 2011
Location: Kansas City, Missouri
Posts: 254
jackwhaines is on a distinguished road
Send a message via Skype™ to jackwhaines
Default

I would take the response from Dest 2 (using the response transformer). Looks like JSON... and save it to a channelMap. Then, in the next destination, read the channelMap variable, parse the JSON, and iterate through each loop to write each row to the database.
__________________

-= Jack Haines : Founder/CEO of Healthcare Integrations, LLC
-= jack.haines@HealthcareIntegrations.com
-= Mirth Connect (Advanced)-certified
-= Gold member of HL7.org
-= Available for Mirth Connect channel development and consultation! Schedule a FREE call with me at https://calendly.com/jackhaines
Reply With Quote
  #3  
Old 10-22-2019, 10:56 AM
tjoyner1 tjoyner1 is offline
Mirth Newb
 
Join Date: Sep 2019
Posts: 10
tjoyner1 is on a distinguished road
Default

That is what I want to do. I can easily map the portion of the xml as a mapper. But it's the syntax in destination 3 I am struggling with... Because I do not know JS well enough... The syntax needs proper structure etc.

Thank you!
Reply With Quote
  #4  
Old 10-22-2019, 11:00 AM
jackwhaines jackwhaines is offline
 
Join Date: May 2011
Location: Kansas City, Missouri
Posts: 254
jackwhaines is on a distinguished road
Send a message via Skype™ to jackwhaines
Default

It would help to see the real data, but I'm guessing something close to...

Code:
var json = JSON.parse(msg['Body'].toString());
__________________

-= Jack Haines : Founder/CEO of Healthcare Integrations, LLC
-= jack.haines@HealthcareIntegrations.com
-= Mirth Connect (Advanced)-certified
-= Gold member of HL7.org
-= Available for Mirth Connect channel development and consultation! Schedule a FREE call with me at https://calendly.com/jackhaines
Reply With Quote
  #5  
Old 10-22-2019, 11:02 AM
tjoyner1 tjoyner1 is offline
Mirth Newb
 
Join Date: Sep 2019
Posts: 10
tjoyner1 is on a distinguished road
Default

Do you mean the full Get response returned by destination 2? I can post it here, but would have to "scrub" PHI first.
Reply With Quote
  #6  
Old 10-22-2019, 11:05 AM
jackwhaines jackwhaines is offline
 
Join Date: May 2011
Location: Kansas City, Missouri
Posts: 254
jackwhaines is on a distinguished road
Send a message via Skype™ to jackwhaines
Default

more complete...

Code:
var json = JSON.parse(msg['Body'].toString());

var dbConn = (connection string here);
var q = "insert into table (nameF, nameL, location, date) values (?,?,?,?)";

for each (j in json)
	{
	var p = new java.util.ArrayList();

	p.add(j['nameF']);
	p.add(j['nameL']);
	p.add(j['location']);
	p.add(j['date']);
	
	dbConn.executeupdate(q,p);
	}

dbConn.close();
__________________

-= Jack Haines : Founder/CEO of Healthcare Integrations, LLC
-= jack.haines@HealthcareIntegrations.com
-= Mirth Connect (Advanced)-certified
-= Gold member of HL7.org
-= Available for Mirth Connect channel development and consultation! Schedule a FREE call with me at https://calendly.com/jackhaines
Reply With Quote
  #7  
Old 10-22-2019, 11:07 AM
jackwhaines jackwhaines is offline
 
Join Date: May 2011
Location: Kansas City, Missouri
Posts: 254
jackwhaines is on a distinguished road
Send a message via Skype™ to jackwhaines
Default

Quote:
Originally Posted by tjoyner1 View Post
Do you mean the full Get response returned by destination 2? I can post it here, but would have to "scrub" PHI first.
I don't need to see the "real" data, but this isn't valid JSON due to the starting [ but no ending ]

Code:
<Body multipart="no">[{"nameF": "John", "nameL": "Doe", "location":
"University", "date": "10/11/2019"}, {"nameF": "Sara", "nameL": "Conner",
"location": "Pixie Land", "date": "10/12/2019"}</Body>
__________________

-= Jack Haines : Founder/CEO of Healthcare Integrations, LLC
-= jack.haines@HealthcareIntegrations.com
-= Mirth Connect (Advanced)-certified
-= Gold member of HL7.org
-= Available for Mirth Connect channel development and consultation! Schedule a FREE call with me at https://calendly.com/jackhaines
Reply With Quote
  #8  
Old 10-22-2019, 12:01 PM
tjoyner1 tjoyner1 is offline
Mirth Newb
 
Join Date: Sep 2019
Posts: 10
tjoyner1 is on a distinguished road
Default

Just for starters on testing ...

I tried =

var json = JSON.parse(msg['Body'].toString());

and I did get an error for what I think is iteration... If I were a betting man, I think this is progress.




Transformer error
ERROR MESSAGE: Error evaluating transformer
com.mirth.connect.server.MirthJavascriptTransforme rException:
CHANNEL: Qgenda API
CONNECTOR: Test File Writer
SCRIPT SOURCE: TRANSFORMER
SOURCE CODE:
629: */
630:
631:
//string delims = "[},{]";
632: //for (int i = 0; i < tokens.length; i++)
633: if ('xml' === typeof msg) {
634: if (msg.hasSimpleContent()) {
635: msg = msg.toXMLString();
636: }
637: } else if ('undefined' !== typeof msg && msg !== null) {
638: var toStringResult = Object.prototype.toString.call(msg);
LINE NUMBER: 634
DETAILS: SyntaxError: Empty JSON string
at 6605265d-2314-4e87-9614-436163b1c9e4:634 (doTransform)
at 6605265d-2314-4e87-9614-436163b1c9e4:715 (doScript)
at 6605265d-2314-4e87-9614-436163b1c9e4:717
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:154)
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:119)
at com.mirth.connect.server.util.javascript.JavaScrip tTask.call(JavaScriptTask.java:113)
at java.util.concurrent.FutureTask.run(FutureTask.jav a:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Reply With Quote
  #9  
Old 10-22-2019, 12:10 PM
jackwhaines jackwhaines is offline
 
Join Date: May 2011
Location: Kansas City, Missouri
Posts: 254
jackwhaines is on a distinguished road
Send a message via Skype™ to jackwhaines
Default

error is from: SyntaxError: Empty JSON string

check and make sure your data types (even in the response types) are accurate

throw some logger.info()'s in there so you can see what data is being passed around...
__________________

-= Jack Haines : Founder/CEO of Healthcare Integrations, LLC
-= jack.haines@HealthcareIntegrations.com
-= Mirth Connect (Advanced)-certified
-= Gold member of HL7.org
-= Available for Mirth Connect channel development and consultation! Schedule a FREE call with me at https://calendly.com/jackhaines
Reply With Quote
  #10  
Old 10-22-2019, 12:11 PM
tjoyner1 tjoyner1 is offline
Mirth Newb
 
Join Date: Sep 2019
Posts: 10
tjoyner1 is on a distinguished road
Default

I feel like there should be included in the JScript .. a for each or something... to handle the part of the XML that repeats...

<Body multipart="no">[{"nameF": "John", "nameL": "Doe", "location":
"University", "date": "10/11/2019"}, {"nameF": "Sara", "nameL": "Conner",
"location": "Pixie Land", "date": "10/12/2019"}


Within the <Body multipart="no"> ... there are sets of repeating fields each series inside a curly bracket and then starting all over again...


{"nameF": "John", "nameL": "Doe", "location":
"University", "date": "10/11/2019"},


{"nameF": "Sara", "nameL": "Conner",
"location": "Pixie Land", "date": "10/12/2019"}

in the real world response... there are many many responses
Reply With Quote
Reply

Tags
api, javascript, mirth connect, xml

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 11:42 AM.


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