Skip to content

Converting JSON to HL7 with a Template

The mirror of Converting HL7 to JSON with a Template. When the target HL7 has a fixed shape and every field maps to a specific JSON element, hold the HL7 body as a template, one line per segment, {alt:/json/path} tags where the JSON values should land, and render it with qie.evaluateTemplate, passing the JSON source as the alternate message so those {alt:...} tags resolve against it.

The array-based alternative (iterating a JSON array in a mapping script and calling addChild per element) is the right choice when the JSON has a variable-length list that expands into repeating HL7 segments. The template approach is the right choice for a fixed HL7 layout mapped one-to-one from JSON fields.

Storing the template in a System Variable

Create a Text-type System Variable named patientAdt with this value (real HL7 segment separators are carriage returns; keep them exactly as they appear in the source system):

MSH|^~\&|QIE|QIE|{alt:/receivingApp}|{alt:/receivingFacility}|{FORMAT_DATE:yyyyMMddHHmmss}||ADT^A08|{MESSAGE_CONTROL_ID}|P|2.5.1
EVN|A08|{FORMAT_DATE:yyyyMMddHHmmss}
PID|1||{alt:/patient/mrn}||{alt:/patient/name/family}^{alt:/patient/name/given}||{alt:/patient/birthDate}|{alt:/patient/gender}

{alt:...} tags read from the alternate message model passed to qie.evaluateTemplate; other tags such as {FORMAT_DATE:...} and {MESSAGE_CONTROL_ID} continue to resolve against the channel's normal bindings.

In a mapping node's Custom script, evaluate that template against the JSON source and assign the rendered HL7 to the outbound message:

var rendered = qie.evaluateTemplate(qie.getVariable('patientAdt'), source);
message.setNode('/', rendered);

The second argument to qie.evaluateTemplate is the alternate message model. Passing source makes the incoming JSON message available to every {alt:...} tag in the template.

Inline template

For a one-off shape that does not need to be reused, keep the template inside the script:

var template =
    'MSH|^~\\&|QIE|QIE|{alt:/receivingApp}|{alt:/receivingFacility}|{FORMAT_DATE:yyyyMMddHHmmss}||ADT^A08|{MESSAGE_CONTROL_ID}|P|2.5.1\r' +
    'PID|1||{alt:/patient/mrn}||{alt:/patient/name/family}^{alt:/patient/name/given}||{alt:/patient/birthDate}|{alt:/patient/gender}\r';

message.setNode('/', qie.evaluateTemplate(template, source));

Use System-Variable storage when the template is reused, exported with a package, or edited by someone who does not want to touch the script.

Field-by-field alternative

When the template is too dynamic to express declaratively, conditional segments, repetition counts driven by the JSON contents, HL7 escape-encoding of embedded delimiters. Build the HL7 message imperatively instead. Create a fresh HL7 message with qie.createHL7Message, populate it with setNode and addChild, and hand it back:

var hl7 = qie.createHL7Message();
hl7.addChild('/', 'MSH');
hl7.setNode('MSH-3', 'QIE');
hl7.setNode('MSH-9.1', 'ADT');
hl7.setNode('MSH-9.2', 'A08');

hl7.addChild('/', 'PID');
hl7.setNode('PID-3.1', source.getNode('/patient/mrn'));
hl7.setNode('PID-5.1', source.getNode('/patient/name/family'));
hl7.setNode('PID-5.2', source.getNode('/patient/name/given'));

message.setNode('/', hl7.toString());

qie.createHL7Message accepts encoding, delimiter, and version arguments if you need something other than the QIE defaults. See the Code Wizard for the full signature.

Escaping literal braces

{ and } are reserved as node-tag delimiters wherever qie.evaluateTemplate runs. A literal brace in the HL7 body (unusual, but possible in free-text fields) must be written as its HTML entity. See Escaping Literal Curly Braces.