For the purpose of this tutorial we will perform some example mappings for a simple JSON object. Paste the following JSON template at the top outside of main to use as the outbound message schema:
Code Block |
---|
local jsonTemplate = [[
{
"sendingFacility": "",
"receivingFacility": "",
"patientId": "",
"firstName": "",
"lastName": "",
"birthdate": ""
}
]] |
Now inside your filter if statement under the comment -- Transform the message to JSON , add the transformation logic: First, parse the JSON template so its ready to be populated with the HL7 data:
Code Block |
---|
-- Transform the message to JSON
local msgOut = json.parse{data=jsonTemplate} |
Create a mapADT.lua file to store your mapping functions. Its good practice to modularize mappings into separate functions for readability and future maintenance. Here we are going to split the mapping into functions for each HL7 segment.
Code Block |
---|
local mapper = {}
-- Map the incoming data to the outgoing JSON
-- Organize mapping functions by HL7 segment
function mapper.mapMSH(MSH, msgOut)
msgOut.sendingFacility = MSH[3][1]:nodeValue()
msgOut.receivingFacility = MSH[6][1]:nodeValue()
return msgOut
end
function mapper.mapPID(PID, msgOut)
msgOut.patientId = PID[3][1]:nodeValue()
msgOut.firstName = PID[5][2]:nodeValue()
msgOut.lastName = PID[5][1][1]:nodeValue()
msgOut.birthdate = PID[7][1]:nodeValue()
msgOut.race = PID[10][1]:nodeValue()
return msgOut
end
return mapper |
Info |
---|
In this example, we are using simple assignment mapping, assigning values from the inbound HL7 message to the outbound JSON. There are many different strategies to map messages. Please see Mapping Messages for more strategies. |
Now call these mapper functions in main : First assign the required mapADT.lua module to the variable mapper so you can call it. Call each mapping function, passing the appropriate HL7 segments.
Code Block |
---|
-- call mapping functions
mapper.mapMSH(msg.MSH, msgOut)
mapper.mapPID(msg.PID, msgOut) |
Use the annotations to see the results of your mappings. You can also use trace(msgOut) to view your JSON in the annotation window at any point during processing. |