Announcement

Collapse
No announcement yet.

Mirth Transformer converts \ to \\ when converting from hl7 to JSON

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

  • 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

  • #2
    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.

    Comment


    • #3
      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)

      Comment


      • #4
        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?

        Comment


        • #5
          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

          Comment


          • #6
            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

            Comment

            Working...
            X