web stats
split batch by Javascript for JSON string - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 08-16-2016, 11:17 PM
pprashant9490 pprashant9490 is offline
What's HL7?
 
Join Date: Aug 2016
Posts: 1
pprashant9490 is on a distinguished road
Question 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.
Reply With Quote
  #2  
Old 09-13-2016, 10:48 PM
mikko mikko is offline
Mirth Newb
 
Join Date: May 2016
Posts: 10
mikko is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 09-14-2016, 08:06 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,119
narupley is on a distinguished road
Default

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

Quote:
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.
Reply With Quote
  #4  
Old 01-31-2018, 12:07 PM
regis.hess regis.hess is offline
OBX.1 Kenobi
 
Join Date: Jun 2014
Posts: 48
regis.hess is on a distinguished road
Default

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

Tags
json, split batch by javascript

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:09 PM.


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