Mapping Messages: HL7 to JSON

In this article we will copy an HL7 message to an JSON document. For simplicity we will only map a few fields, but as you will see extra fields can easily be added. We placed the mapping code in Filter component, which is (usually) the most logical place for mapping code.

You can use exactly the same method to copy data between any of the different types of node trees.

These are the steps:

  1. Parse the message into a source HL7 node tree structure
  2. Create a JSON table tree for the target message
  3. Map the data from the source node tree to the target node tree

We recommend that you type in the code for each step, but we also include complete Sample Code if you prefer to paste it in and follow along.

Tip: If you want to modify the message data see the “Transforming Messages:” tutorials in this section. Mapping and transformation use very similar techniques, the main difference is the intention: When you are mapping you are just copying the data, when you are transforming a message you are modifying the data (as well as copying it).

Create the Channel [top]

  1. Create a Channel with the the following settings:
    • Source = LLP Listener
    • Destination = To Translator
    • Channel name = Mapping HL7 to JSON
  2. Activate the Filter Component: Click the Filter tab and check Use Filter.
  3. Click the Add Channel button to create the channel.
    Ignore the red warning messages, see resolving the milestone configuration error.
  4. Open the Translator by clicking the Edit Script link at the bottom of the Filter tab.
  5. Download and import the Mapping_HL7_to_JSON_Filter.zip project file.
    This file contains a skeleton project and six sample HL7 messages.
  6. Iguana will load the project and data into the Translator, your screen should look like this:

Tutorial Instructions [top]

  1. Pass the message data to the script.
    • Iguana automatically passes the message data to the main() function
    • The message can be accessed using the Data parameter
    • No action is needed
  2. Parse the message into a convenient read-onlynode tree” structure.
    Add the following line of code to your script:
  3. Compare the text message and the parsed message.
    Click on the Data parameter (red text) and the ADT message icon:

    The messages should look something like this (we clicked on the PID segment to expand it):
  4. Create the JSON outgoing message.
    Add the following line of code to your script:
  5. Map the HL7 node PID>Patient Identifier to the XML patient attribute id, using the :nodeValue() method to convert the node to text.
    Enter this line of mapping code, auto-completion will help you to find the fields:
  6. Complete the patient mapping.
    Add these three lines of code:
  7. Compare the incoming and outgoing messages.
    Add a trace() command and click on the Msg (ADT) and Out (table) annotations to view them:

    As you can see the mapping worked correctly, the patient data was mapped from the incoming to the outgoing message.
    Screen Shot 2014-10-07 at 22.03.21
  8. Push the outgoing message into the Iguana queue, using json.serialize() to convert the Out message into JSON formatted text:
    Add the following code to your script:

    Note: This is not part of the mapping process, but it is needed to forward the message to the Destination Component.

Complete Sample Code [top]

Here is the completed mapping code that you can cut and paste into your script:

local JSON_TEMPLATE = [[
{
   'patients':
   {
      'patient-id':'',
      'first-name':'',
      'last-name':'',
      'social-security-no':''
   }
}]]

function main(Data)
   -- (1) Parse the HL7 message
   local Msg = hl7.parse{vmd = 'example/demo.vmd', data = Data}

   -- (2) parse the JSON template
   local Out = json.parse(JSON_TEMPLATE)
   
   -- (3) Map (part of) the message
   Out.patients["patient-id"]         = Msg.PID[3][1][1]:nodeValue()
   Out.patients["first-name"]         = Msg.PID[5][1][1][1]:nodeValue()
   Out.patients["last-name"]          = Msg.PID[5][1][2]:nodeValue()
   Out.patients["social-security-no"] = Msg.PID[19]:nodeValue()
   
   -- (3) Push the outgoing message into the Iguana queue
   queue.push(json.serialize{data=Out, compact=true})
end

More Information [top]

Leave A Comment?