Announcement

Collapse
No announcement yet.

Need help parsing XML using Javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    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
    -= [email protected]
    -= 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

    Comment


    • #3
      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!

      Comment


      • #4
        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
        -= [email protected]
        -= 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

        Comment


        • #5
          Do you mean the full Get response returned by destination 2? I can post it here, but would have to "scrub" PHI first.

          Comment


          • #6
            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
            -= [email protected]
            -= 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

            Comment


            • #7
              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
              -= [email protected]
              -= 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

              Comment


              • #8
                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)

                Comment


                • #9
                  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
                  -= [email protected]
                  -= 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

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      Okay, about end of day here, I'll have more time tomorrow.

                      Thank you very much!

                      Comment


                      • #12
                        <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



                        In my second destination i have a channel mapper

                        var GRB = msg['Body'].toString();
                        channelMap.put('GRB', GRB)

                        In my third destination a transformer *you helped me with

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

                        I then get the error I posted earlier...


                        635: var GRB = msg['Body'].toString().split('{}');
                        636: for (var i = 0; i < GRB.length(); i++) {
                        637: GRB += (msg['Body'][i]['{}'].toString() + '\n')
                        638: channelMap.put('GRB', GRB)
                        LINE NUMBER: 634
                        DETAILS: SyntaxError: Empty JSON string


                        Thank you for your help!
                        Last edited by tjoyner1; 10-23-2019, 03:48 AM.

                        Comment


                        • #13
                          It looks like you are still treating the data as a string and trying to parse it yourself. Let JSON.parse to the heavy lifting for you and deal with the JSON objects one by one. Also, add in some debugging with logger.info() in EACH step so you can "see" what Mirth "sees".

                          -= Jack Haines : Founder/CEO of Healthcare Integrations, LLC
                          -= [email protected]
                          -= 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

                          Comment

                          Working...
                          X