Announcement

Collapse
No announcement yet.

Mirth Connect CSV File to JSON

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

  • Mirth Connect CSV File to JSON

    Is it possible to use Mirth Connect to pick up a .csv file on a local drive, transform it to JSON, and send the information to another system using HTTP Sender, POST?

  • #2
    Originally posted by cherribuggy View Post
    Is it possible to use Mirth Connect to pick up a .csv file on a local drive, transform it to JSON, and send the information to another system using HTTP Sender, POST?
    Yes, that is very much possible, and quite easy in fact. Here's an example:

    Inbound Message

    Code:
    FIRST_NAME,LAST_NAME,MRN,DOB,GENDER,SSN
    John,Doe,11111,19900101,M,123456789
    Jane,Doe,22222,19920202,F,234567890
    Transformer

    Code:
    var header = msg.row[0].children();
    var jsonObject = {'patients':[]};
    for (var i = 1; i <= msg.row.length()-1; i++) {
    	var patient = {};
    	for (var j = 0; j <= header.length()-1; j++)
    		patient[header[j].toString()] = msg.row[i].children()[j].toString();
    	jsonObject.patients.push(patient);
    }
    
    $co('patients',JSON.stringify(jsonObject));
    Then you just include ${patients} in your HTTP Sender template and set the content type to application/json. Obviously that also requires inclusion of json.js.

    EDIT: Actually you don't need to include json.js at all, Rhino includes a JSON parser by default.
    Attached Files
    Last edited by narupley; 01-28-2014, 09:38 AM.
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      Inclusion of json.js

      Thank you for the information! I'm sorry, this is all new to me, could you explain including json.js?

      Comment


      • #4
        Originally posted by cherribuggy View Post
        Thank you for the information! I'm sorry, this is all new to me, could you explain including json.js?
        Just include the file itself using the External Script transformer type. It's in the example channel that I attached.
        Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

        Nicholas Rupley
        Work: 949-237-6069
        Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


        - How do I foo?
        - You just bar.

        Comment


        • #5
          Attempting to set up CSV to JSON Channel

          Thank you for your answers concerning this. This is the first time I've used Mirth and I'm trying to evaluate if it will be a solution in our environment. I think I've gotten pretty far with the set up and tried to deploy the channel. It didn't seem to pick up the file and process it though, so I've obviously missed something important. I did place the file json.js in the directory indicated in the transformer. I exported the channel and also have attached the file I'm attempting to process (saved with .txt extension instead of .cvs). Would you be able to tell me what I've missed?

          I should also add the error message!
          Server Log: Error (com.mirth.connect.connectors.file.FileConnector:3 56): Could not create URI from endpoint: file://c:/Restock%20Files
          Attached Files
          Last edited by cherribuggy; 07-10-2013, 10:53 AM. Reason: Added error message on server log

          Comment


          • #6
            There are multiple issues with that setup. First, did you mean ".csv" instead of ".cvs"? As long as it's the same extension in both places it doesn't matter, just thought I'd check.

            Another issue is that you have your data types set to HL7 v2.x, yet you're trying to read in delimited text.

            Also, you're processing batch files, meaning that (if you had Delimited Text set with a batch option set) each row would get dispatched to the channel as a discrete message. It sounds like that's not what you want, but I could be wrong. If you do process batch files, that will change the code required in the transformer, so beware. As long as you can handle tweaking some simple JavaScript it should be fine though.

            In your HTTP Sender, you're including the ${orders} string directly in the URL. Is there a reason for that? That will almost certainly cause the request to fail, unless what you're appending is a properly formatted query parameter string.

            Fix all those things and it should work. Or, just use the channel I attached and tweak the directories, etc. so you can at least do a proof-of-concept.
            Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

            Nicholas Rupley
            Work: 949-237-6069
            Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


            - How do I foo?
            - You just bar.

            Comment


            • #7
              ERROR-404: HTTP Connector Error

              Error Message: Object Reference not set to an instance of an object

              This is what I have in the transformer:
              var header = msg.row[0].children();
              var jsonObject = {'orders':[]};
              for (var i = 1; i <= msg.row.length()-1; i++) {
              var order = {};
              for (var j = 0; j <= header.length()-1; j++)
              order[header[j].toString()] = msg.row[i].children()[j].toString();
              jsonObject.orders.push(order);
              }

              $co('orders',JSON.stringify(jsonObject));



              I'm so close!!! Thank you for all your help today.

              Comment


              • #8
                Originally posted by cherribuggy View Post
                Error Message: Object Reference not set to an instance of an object

                This is what I have in the transformer:
                var header = msg.row[0].children();
                var jsonObject = {'orders':[]};
                for (var i = 1; i <= msg.row.length()-1; i++) {
                var order = {};
                for (var j = 0; j <= header.length()-1; j++)
                order[header[j].toString()] = msg.row[i].children()[j].toString();
                jsonObject.orders.push(order);
                }

                $co('orders',JSON.stringify(jsonObject));



                I'm so close!!! Thank you for all your help today.
                Can you include the updated channel you're using, and the full error stacktrace?
                Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

                Nicholas Rupley
                Work: 949-237-6069
                Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


                - How do I foo?
                - You just bar.

                Comment


                • #9
                  Channel and Mirth.log

                  I couldn't seem to find how to get the full error stack trace. Here is what I have.
                  Attached Files

                  Comment


                  • #10
                    Originally posted by cherribuggy View Post
                    I couldn't seem to find how to get the full error stack trace. Here is what I have.
                    That works fine for me. In that log file, I see the following:

                    Code:
                    ERROR 2013-07-10 17:32:38,036 [ddd26910-3fe3-4253-b504-f2d4f6bce836_source_connector._fileEndpoint#-1632967682.receiver.1] com.mirth.connect.connectors.file.FileConnector: Could not create URI from endpoint: file://c:/Restock%20Files
                    The issue is that you don't have read access to that directory.
                    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

                    Nicholas Rupley
                    Work: 949-237-6069
                    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


                    - How do I foo?
                    - You just bar.

                    Comment


                    • #11
                      Perhaps this is a very stupid question. But I will ask it anyway.

                      Why do I need to import JSON.js?
                      I did a little test and as far as I know JSON.stringify is already usable.

                      with kind regards
                      Stefan Scholte
                      Stefan

                      Mirth Certified|Epic Bridges Certified|Cloverleaf Level 2 Certified

                      Comment


                      • #12
                        I myself use JSON.stringify without importing anything at all.
                        I think that Rhino has the JSON Object already built-in. On a REPL console of Rhino, this works: print(JSON.stringify({"teste":[]}));
                        Last edited by santosam; 07-15-2013, 06:55 AM. Reason: typo

                        Comment


                        • #13
                          You guys are right, Rhino is now compatible with all features of ECMAScript 5, including a native JSON parser. That wasn't always the case; it was added in 1.7R3, which came out mid-2011.
                          Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

                          Nicholas Rupley
                          Work: 949-237-6069
                          Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


                          - How do I foo?
                          - You just bar.

                          Comment


                          • #14
                            How could I set this up to monitor an FTP site rather than a file folder

                            I have this channel set up and it is picking up files deposited in a folder on a drive, how could I set the source up to monitor an FTP site instead? Thank you for all your help last week!

                            Comment


                            • #15
                              Originally posted by cherribuggy View Post
                              I have this channel set up and it is picking up files deposited in a folder on a drive, how could I set the source up to monitor an FTP site instead? Thank you for all your help last week!
                              Switch the File Reader from "file" mode to "ftp", and fill in the necessary information.
                              Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

                              Nicholas Rupley
                              Work: 949-237-6069
                              Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


                              - How do I foo?
                              - You just bar.

                              Comment

                              Working...
                              X