Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   Mirth Transformer converts \ to \\ when converting from hl7 to JSON (http://www.mirthcorp.com/community/forums/showthread.php?t=219265)

Golfmachine 07-18-2019 03:01 AM

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

agermano 07-18-2019 10:48 AM

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.

Golfmachine 07-22-2019 10:46 AM

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)

Golfmachine 07-22-2019 10:52 AM

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?

agermano 07-22-2019 01:47 PM

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


agermano 07-22-2019 01:56 PM

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



All times are GMT -8. The time now is 03:16 PM.

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