How to parse multiple OBX segments in Mirth
Often there is a need to parse segments that occur multiple time in an HL7 message like OBX or Ali segments. For example, in an ADT message you will get height and weight in two OBX segments. How do you parse these multiple OBX segments and make the available to the channel map? I am going to show you how to parse multiple OBX segments in an ADT message and populate variables in to a channel map so they are accessible for further manipulation in the destination connector….
For brevity sake I am just going to showing the OBX segments that I am going to parse here instead of the whole message. The following two OBX segments are present in my message:
OBX|1|ST|HT^PATIENT HEIGHT||142.9
OBX|2|ST|WT^PATIENT WEIGHT||45.2
I want to create three variables, one to hold the height, one to hold the weight and one to hold the number of segments we parsed.
The first step is to create the channel. I am assuming here you already know how to create a channel. For my channel l have my source as a file reader and a database reader as the destination. I create a transformer for the destination. In the transformer, I paste my sample message in the template section and create a step with step being “Javascript”. Then I type the following script as my Java script:
var i=0;
for each (seg in msg..OBX)
{
i++;
var key = seg['OBX.3']['OBX.3.2'].toString()
var obx_val = seg['OBX.5']['OBX.5.1'].toString() ;
if(key.search(/HEIGHT/) != -1)channelMap.put(‘HEIGHT’, obx_val);
if(key.search(/WEIGHT/) != -1)channelMap.put(‘WEIGHT’, obx_val);
}
channelMap.put(“obx_seg_count”, i);
This script creates three variables, HEIGHT, WEIGHT and “obx_seg_count” that are now available to us in the destination for inserting in to a data base as follows.
Assuming I have a table called ‘OBX’ and columns called ‘ht’ and ‘wt’, I insert the values in to the table as follows in the detonation tab….
expression = “insert into OBX (ht, wt ) values (‘” + $(‘HEIGHT’) + “‘ , ‘” + $(‘WEIGHT’) + “‘)”;
var result = dbConn.executeUpdate(expression);
The trick in this is the for each loop above. The expression in the parenthesis is simple XML E4X expression (see https://wso2.org/project/mashup/0.2/docs/e4xquickstart.html for more details). The other concept to grasp here is to note that the incoming message is available as ‘msg’ object and is just a XML object. Knowing this we can traverse the XML object with E4X and find the elements we are interested in which are OBX elements. The other concept to understand is the ‘maps’ in the Mirth. You need to populate the ‘channel map’ so those variable you create are available throughout your channel.
If you have unknown number of OBX segments in a message, like in a ORU message where you get a report, you can do two things, we can either loop through the way I did up here and concatenate each OBX segment to create on variable called ‘report’ with <cr> at the end and put that in to channel map. Or you could create several variables like following….
var i=0;
for each (seg in msg..OBX)
{
i++;
var key = “report_seg_”+I;
var obx_val = seg['OBX.5']['OBX.5.1'].toString() ;
channelMap.put(key, obx_val);
}
channelMap.put(“obx_seg_count”, i);
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Comments
No comments yet.
Leave a comment