web stats
Mirth Transformer converts \ to \\ when converting from hl7 to JSON - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 07-18-2019, 02:01 AM
Golfmachine Golfmachine is offline
What's HL7?
 
Join Date: Jun 2019
Posts: 4
Golfmachine is on a distinguished road
Post Mirth Transformer converts \ to \\ when converting from hl7 to JSON

Hi all! I am currently having problems with my transformer. I currently have an OBX line wherein line breaks are denoted by "\n" as follows:

OBX|1|INT|| \n \n \n \n \nNormal sinus rhythm \n \nPoor R wave progression V1-V6

I have a transformer where the inbound is in hl7 and outbound is in JSON. I am using a JSON template for this. Using a message builder, the generated script was:
tmp['data']['result']['results_data'][i]['value'] = validate(msg['OBX'][i]['OBX.4']['OBX.4.1'].toString(), '', new Array());

I was expecting the JSON value to contain the following:
\n \n \n \n \nNormal sinus rhythm \n \nPoor R wave progression V1-V6
However, the JSON files that come out are
\\n \\n \\n \\n \\nNormal sinus rhythm \\n \\nPoor R wave progression V1-V6

I did several tests and my conclusion has been that the generated script creates \\ in the JSON for every \ in the hl7. I have tried using \\ in the hl7 and JSON just becomes \\\\.

My question would be, how do I convert from hl7 to JSON with each \ staying as \ in the JSON and not \\?

Thank you
Reply With Quote
  #2  
Old 07-18-2019, 09:48 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 841
agermano is on a distinguished road
Default

That's to be expected. The backslash character needs to be escaped in json. (and technically, it should be in hl7, too, if it's already specified as the escape character.)

When the json is parsed into an object, the extra backslash will be removed.
Reply With Quote
  #3  
Old 07-22-2019, 09:46 AM
Golfmachine Golfmachine is offline
What's HL7?
 
Join Date: Jun 2019
Posts: 4
Golfmachine is on a distinguished road
Default

Just saw the reply now. Sorry but I'm actually not sure how to parse the JSON to an object? I tried to make a new javascrit step with:

var obj = JSON.parse(tmp['data']['result']['results_data'][i]['value'])

All I get is the following error:
DETAILS: SyntaxError: Unexpected token: \
at dec9c529-a782-4371-a68e-0571c3ad1a93:889 (doTransform)
at dec9c529-a782-4371-a68e-0571c3ad1a93:932 (doScript)
at dec9c529-a782-4371-a68e-0571c3ad1a93:934
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(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
at java.lang.Thread.run(Unknown Source)
Reply With Quote
  #4  
Old 07-22-2019, 09:52 AM
Golfmachine Golfmachine is offline
What's HL7?
 
Join Date: Jun 2019
Posts: 4
Golfmachine is on a distinguished road
Default

Just saw the reply now. Sorry but I'm actually not sure how to parse the JSON to an object? I tried to make a new javascrit step with:

var obj = JSON.parse(tmp['data']['result']['results_data'][i]['value'])

All I get is the following error:
DETAILS: SyntaxError: Unexpected token: \
at dec9c529-a782-4371-a68e-0571c3ad1a93:889 (doTransform)
at dec9c529-a782-4371-a68e-0571c3ad1a93:932 (doScript)
at dec9c529-a782-4371-a68e-0571c3ad1a93:934
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(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
at java.lang.Thread.run(Unknown Source)

Update:
I tried using the same code but with a different a JSON entry containing no \ and no error ocurred. It seems that the \ is preventing it from parsing to an object?
Reply With Quote
  #5  
Old 07-22-2019, 12:47 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 841
agermano is on a distinguished road
Default

You don't have to. It will happen at the remote end.

Code:
var o = {};
o.value = msg['OBX'][0]['OBX.4']['OBX.4.1'].toString();

// This is how it will look after your transformer is finished
var json = JSON.stringify(o);
logger.info(json);

// This is how it will look to the remote end using your returned json
var parsed = JSON.parse(json);
logger.info(parsed.value);
I used your sample segment in my message. This is the output:
Code:
1 [main] INFO root  - {"value":" \\n \\n \\n \\n \\nNormal sinus rhythm \\n \\nPoor R wave progression V1-V6"}
3 [main] INFO root  -  \n \n \n \n \nNormal sinus rhythm \n \nPoor R wave progression V1-V6
Reply With Quote
  #6  
Old 07-22-2019, 12:56 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 841
agermano is on a distinguished road
Default

If I misunderstood your problem, and you actually want to convert the "\n" to new lines rather than passing the sequence through, you can do a string replace on them.

Code:
var o = {};
o.value = msg['OBX'][0]['OBX.4']['OBX.4.1'].toString();

// convert '\n' character sequence to new lines
o.value = o.value.replace(/\\n/g, '\n');

// this is how it will look after your transformer is finished
var json = JSON.stringify(o);
logger.info(json);

// This is how it will look to the remote end using your returned json
var parsed = JSON.parse(json);
logger.info(parsed.value);
The above code produces the following output:
Code:
0 [main] INFO root  - {"value":" \n \n \n \n \nNormal sinus rhythm \n \nPoor R wave progression V1-V6"}
3 [main] INFO root  -  
 
 
 
 
Normal sinus rhythm 
 
Poor R wave progression V1-V6
Reply With Quote
Reply

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 12:17 PM.


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