Announcement

Collapse
No announcement yet.

split batch by Javascript for JSON string

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

  • split batch by Javascript for JSON string

    Hi,
    I have inbound JSON string as follows,

    {
    "action": "add_billed_code",
    "tenantCode": "ACH",
    "encounterList": [
    {
    "encounterNumber": "3678754",
    "mrn": "000645230",
    "codeList": [
    {
    "type": "Secondary",
    "statusCode": "Original",
    "ordinality": "9999",
    "code": "71010",
    "procedureDateTime": "20160417",
    "unitAmount": "166.00",
    "quantity": "1",
    "extendedAmount": "166.00",
    "chargeCode": "4401010",
    "deptCode": "4040",
    "modifier1": "TC",
    "revenueCode": "0324",
    "providerId": "92038"
    }
    ]
    },
    {
    "encounterNumber": "3678730",
    "mrn": "000804678",
    "codeList": [
    {
    "type": "Secondary",
    "statusCode": "Original",
    "ordinality": "9999",
    "code": "73110",
    "procedureDateTime": "20160416",
    "unitAmount": "199.00",
    "quantity": "1",
    "extendedAmount": "199.00",
    "chargeCode": "4409360",
    "deptCode": "4040",
    "modifier1": "TC",
    "revenueCode": "0320",
    "providerId": "92038"
    }
    ]
    },
    {
    "encounterNumber": "3678671",
    "mrn": "000605067",
    "codeList": [
    {
    "type": "Secondary",
    "statusCode": "Original",
    "ordinality": "9999",
    "code": "84443",
    "procedureDateTime": "20160416",
    "unitAmount": "148.25",
    "quantity": "1",
    "extendedAmount": "148.25",
    "chargeCode": "4110886",
    "deptCode": "4011",
    "revenueCode": "0301",
    "providerId": "00701"
    }
    ]
    },
    {
    "encounterNumber": "3678577",
    "mrn": "000608599",
    "codeList": [
    {
    "type": "Secondary",
    "statusCode": "Original",
    "ordinality": "9999",
    "code": "84443",
    "procedureDateTime": "20160416",
    "unitAmount": "69.50",
    "quantity": "1",
    "extendedAmount": "69.50",
    "chargeCode": "4171812",
    "deptCode": "4011",
    "revenueCode": "0301",
    "providerId": "95544"
    }
    ]
    }
    ]
    }

    As we can see, I have an encounterList which has list of encounter identified by encounterNumber. I wanted to create and dispatch a separate message for each encounter in the list. I am using split batch by Javascript in mirth connect. Can anyone guide me on the Javascript code.?? Thanks in advance.

  • #2
    Same here, I'm kinda lost on how to handle JSON string in a channel reader, wanted to split it to multiple messages. Can't find a documentation about it.

    Comment


    • #3
      A description of the JavaScript batch method is found right there in the data types dialog.

      Enter JavaScript that splits the batch, and returns the next message. This script has access to 'reader', a Java BufferedReader, to read the incoming data stream. The script must return a string containing the next message, or a null/empty string to indicate end of input. This option has no effect unless Process Batch is enabled in the connector.
      The easy way is to just read in the entire message in memory, parse it from JSON into an object, and then return a message for each encounter:

      Code:
      var queue = $gc('queue');
      if (!queue) {
      	queue = new java.util.LinkedList();
      	var obj = JSON.parse(org.apache.commons.io.IOUtils.toString(reader));
      	for each (encounter in obj.encounterList) {
      		queue.add(JSON.stringify(encounter));
      	}
      	$gc('queue', queue);
      }
      
      var message = queue.poll();
      if (!message) {
      	globalChannelMap.remove('queue');
      }
      return message;
      If you don't want to read the entire message into memory at once, you can use the batch script to read one character at a time, and then return a message every time it finds a complete encounter.

      Code:
      var readerHash = $gc('readerHash');
      if (readerHash != reader.hashCode()) {
      	globalChannelMap.remove('foundList');
      	$gc('readerHash', new java.lang.Integer(reader.hashCode()));
      }
      
      var foundList = $gc('foundList') == true;
      var inEncounter = false;
      var message = new java.lang.StringBuilder();
      var depth = 0;
      
      var c;
      while ((c = reader.read()) != -1) {
      	if (foundList) {
      		var ch = java.lang.Character.valueOf(c);
      		if (inEncounter) {
      			message.append(ch);
      			if (ch == '{') {
      				depth++;
      			} else if (ch == '}' && --depth == 0) {
      				break;
      			}
      		} else if (ch == ']') {
      			break;
      		} else if (ch == '{') {
      			inEncounter = true;
      			depth++;
      			message.append(ch);
      		}
      	} else if (check('"encounterList"')) {
      		foundList = true;
      		$gc('foundList', foundList);
      	}
      }
      
      return message.toString();
      
      function check(str) {
      	var array = new java.lang.String(str).toCharArray();
      	reader.mark(array.length);
      	for (var i = 0; i < array.length; i++) {
      		var c = reader.read();
      		if (c == -1 || array[i] != java.lang.Character.valueOf(c).charValue()) {
      			reader.reset();
      			return false;
      		}
      	}
      	return true;
      }
      Note that the above really only matters for the File Reader. Currently the other source connectors don't "stream" data, so the message is going to be read fully into memory anyway. The only exception is the TCP Listener when the inbound data type is HL7 v2.x.
      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.

      Comment


      • #4
        Hi, I'm currently using a destination connector on one channel to GET JSON data from a URL, and then I pass it into a channel writer by pulling from the response map. I am having a problem on the next channel I pass it into. Using the code above, the data is simply ignored rather than parsed. Can anybody give me any guidance here? I'd be happy to share my code.

        Comment

        Working...
        X