web stats
Mirth Connect CSV File to JSON - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2013, 05:43 AM
cherribuggy cherribuggy is offline
Mirth Newb
 
Join Date: Jul 2013
Location: Pittsburgh, PA
Posts: 8
cherribuggy is on a distinguished road
Default 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?
Reply With Quote
  #2  
Old 07-10-2013, 06:56 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,116
narupley is on a distinguished road
Default

Quote:
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
File Type: xml Example - CSV to JSON POST.xml (10.2 KB, 330 views)
File Type: txt test.csv.renameme.txt (111 Bytes, 109 views)
__________________
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.

Last edited by narupley; 01-28-2014 at 09:38 AM.
Reply With Quote
  #3  
Old 07-10-2013, 07:34 AM
cherribuggy cherribuggy is offline
Mirth Newb
 
Join Date: Jul 2013
Location: Pittsburgh, PA
Posts: 8
cherribuggy is on a distinguished road
Default Inclusion of json.js

Thank you for the information! I'm sorry, this is all new to me, could you explain including json.js?
Reply With Quote
  #4  
Old 07-10-2013, 07:47 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,116
narupley is on a distinguished road
Default

Quote:
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.
Reply With Quote
  #5  
Old 07-10-2013, 10:48 AM
cherribuggy cherribuggy is offline
Mirth Newb
 
Join Date: Jul 2013
Location: Pittsburgh, PA
Posts: 8
cherribuggy is on a distinguished road
Default 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
File Type: xml JSON in HTTP request channel export.xml (8.3 KB, 46 views)
File Type: txt Pharm_PO_Order_Test_20130710.txt (1.3 KB, 35 views)

Last edited by cherribuggy; 07-10-2013 at 10:53 AM. Reason: Added error message on server log
Reply With Quote
  #6  
Old 07-10-2013, 11:00 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,116
narupley is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 07-10-2013, 12:06 PM
cherribuggy cherribuggy is offline
Mirth Newb
 
Join Date: Jul 2013
Location: Pittsburgh, PA
Posts: 8
cherribuggy is on a distinguished road
Default 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.
Reply With Quote
  #8  
Old 07-10-2013, 01:14 PM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,116
narupley is on a distinguished road
Default

Quote:
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.
Reply With Quote
  #9  
Old 07-10-2013, 01:49 PM
cherribuggy cherribuggy is offline
Mirth Newb
 
Join Date: Jul 2013
Location: Pittsburgh, PA
Posts: 8
cherribuggy is on a distinguished road
Default 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
File Type: xml JSON in HTTP request Export for HTTP Error.xml (23.8 KB, 45 views)
File Type: txt mirth.log_Sav.txt (160.1 KB, 17 views)
Reply With Quote
  #10  
Old 07-10-2013, 01:55 PM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,116
narupley is on a distinguished road
Default

Quote:
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.
Reply With Quote
Reply

Tags
csv, http sender, json

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 05:04 AM.


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