Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   Mirth Connect CSV File to JSON (http://www.mirthcorp.com/community/forums/showthread.php?t=9016)

cherribuggy 07-10-2013 05:43 AM

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?

narupley 07-10-2013 06:56 AM

2 Attachment(s)
Quote:

Originally Posted by cherribuggy (Post 33504)
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.

cherribuggy 07-10-2013 07:34 AM

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

narupley 07-10-2013 07:47 AM

Quote:

Originally Posted by cherribuggy (Post 33509)
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.

cherribuggy 07-10-2013 10:48 AM

Attempting to set up CSV to JSON Channel
 
2 Attachment(s)
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

narupley 07-10-2013 11:00 AM

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.

cherribuggy 07-10-2013 12:06 PM

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.

narupley 07-10-2013 01:14 PM

Quote:

Originally Posted by cherribuggy (Post 33515)
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?

cherribuggy 07-10-2013 01:49 PM

Channel and Mirth.log
 
2 Attachment(s)
I couldn't seem to find how to get the full error stack trace. Here is what I have.

narupley 07-10-2013 01:55 PM

Quote:

Originally Posted by cherribuggy (Post 33517)
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.


All times are GMT -8. The time now is 02:28 AM.

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