GPT converts HL7 v2.4 to FHIR R4 in the Mirth integration engine and many other interoperability tools.
We recently conducted an internal study on the use of AI in healthcare interoperability and came across an exciting experiment. It provided the first proof that ChatGPT can take on some of the tasks typically handled by an integration engineer.
In early 2023, our “industry colleague” Zhong Li, from the Intersystems community, explored GPT’s capabilities by asking it to provide a code example for converting an HL7 message to FHIR. The response he received is definitely worth sharing with you.
Question: Can you create a sample transformation converting HL7 v2.4 to FHIR R4 in Mirth integration engine?
ChatGPT answer in 2023
This is the answer that ChatGPT provided back in 2023:
Certainly! Here’s an example of how to transform an HL7 V2.4 message to a FHIR R4 Observation resource in Mirth:
- First, create a new Mirth channel and add an HL7v2 Listener source connector to receive the input HL7 V2 message.
- Add a new JavaScript Writer destination connector to the channel and configure it to use a custom script.
- In the JavaScript Writer connector, add the following script to transform the HL7 V2 message to a FHIR R4 Observation resource:
- Save the script and test the transformation by sending an HL7 V2 message to the channel. The transformed FHIR R4 Observation resource should be output by the JavaScript Writer connector in JSON format.
var hl7msg = msg['HL7v2 Message'].toString(); // get the HL7 V2 message
var hl7data = Packages.ca.uhn.hl7v2.parser.Parser.parse(hl7msg); // parse the message with the HAPI HL7 parser
// extract values from the HL7 message and create the FHIR resource
var obs = new Packages.org.hl7.fhir.r4.model.Observation();
obs.setStatus(Packages.org.hl7.fhir.r4.model.Observation.ObservationStatus.FINAL);
var code = new Packages.org.hl7.fhir.r4.model.CodeableConcept();
code.addCoding()
.setSystem("http://loinc.org")
.setCode(hl7data.get("OBR-4-1").getValue())
.setDisplay(hl7data.get("OBR-4-2").getValue());
obs.setCode(code);
var value = new Packages.org.hl7.fhir.r4.model.Quantity();
value.setValue(hl7data.get("OBX-5-1").getValue());
value.setUnit(hl7data.get("OBX-6").getValue());
obs.setValue(value);
var subject = new Packages.org.hl7.fhir.r4.model.Reference();
subject.setReference("Patient/" + hl7data.get("PID-3-1").getValue());
obs.setSubject(subject);
// output the FHIR resource as JSON
var json = org.hl7.fhir.r4.formats.JsonParser().setPrettyPrint(true).encodeResourceToString(obs);
return json;
This script uses the HAPI FHIR and HAPI HL7 v2 libraries, which are included with Mirth. It starts by extracting the HL7 V2 message from the Mirth message object and parsing it with the HAPI HL7 parser.
Next, the script creates a new FHIR R4 Observation resource and sets its status, code, value, and subject elements based on the corresponding values in the HL7 message.
Finally, the script serializes the FHIR resource as JSON and returns it.
Note that this is just a simple example and may not work for all HL7 V2 message types or FHIR resources. You may need to modify the script to handle different message types or map additional fields to the FHIR resource.
Original source of the response.
Intriguing, isn’t it?
How many years did you spend mastering programming languages and healthcare protocol standards to deliver such precise responses? We took on the challenge and decided to see how much our AI friend (GPT) has evolved over the past 1.5 years.
ChatGPT answer in 2024
We posed the same question to GPT again, and here is the answer:
Creating a transformation to convert HL7 v2.4 to FHIR R4 in the Mirth integration engine requires mapping each relevant HL7 segment to its corresponding FHIR resource format. Below is a sample JavaScript transformation script for Mirth Connect, focusing on patient demographics from an HL7 ADT (Admit, Discharge, Transfer) message to a FHIR Patient resource.
Sample Transformation Script
- Open Mirth Connect and create a new channel.
- Configure the Source Connector to accept HL7 v2.4 messages.
- Use the Transformer in the source to apply the following script to convert HL7 segments to a FHIR JSON structure.
JavaScript Transformation (HL7 v2.4 to FHIR R4 – Patient Resource)
// Initialize FHIR Patient Resource
var patientResource = {
resourceType: "Patient",
identifier: [],
name: [],
telecom: [],
gender: "",
birthDate: ""
};
// Map HL7 PID Segment to FHIR Patient Resource
var pid = msg['PID'];
// Patient Identifier (from PID-3)
if (pid['PID.3']['PID.3.1'].toString()) {
patientResource.identifier.push({
use: "usual",
system: "urn:oid:1.2.36.146.595.7.3.1.1",
value: pid['PID.3']['PID.3.1'].toString()
});
}
// Patient Name (from PID-5)
if (pid['PID.5']['PID.5.1'].toString()) {
patientResource.name.push({
use: "official",
family: pid['PID.5']['PID.5.1'].toString(),
given: [pid['PID.5']['PID.5.2'].toString()]
});
}
// Telecom - Phone Number (from PID-13)
if (pid['PID.13']['PID.13.1'].toString()) {
patientResource.telecom.push({
system: "phone",
value: pid['PID.13']['PID.13.1'].toString(),
use: "home"
});
}
// Gender (from PID-8)
switch (pid['PID.8'].toString()) {
case 'M':
patientResource.gender = "male";
break;
case 'F':
patientResource.gender = "female";
break;
default:
patientResource.gender = "unknown";
}
// Birth Date (from PID-7)
if (pid['PID.7']['PID.7.1'].toString()) {
var birthDate = pid['PID.7']['PID.7.1'].toString();
patientResource.birthDate = birthDate.substring(0, 4) + '-' +
birthDate.substring(4, 6) + '-' +
birthDate.substring(6, 8);
}
// Output FHIR Patient Resource as JSON
return JSON.stringify(patientResource, null, 2);
Explanation of Key Transformations
- Identifier (PID-3): Maps the patient identifier from HL7 to the identifier array in the FHIR Patient resource.
- Name (PID-5): Constructs the name array with family and given names.
- Telecom (PID-13): Adds the patient’s phone number to the telecom array.
- Gender (PID-8): Translates gender values (M/F) from HL7 to FHIR-compliant terms.
- Birth Date (PID-7): Reformats the birth date from HL7 to the FHIR YYYY-MM-DD format.
Additional Notes
- Ensure that the Destination Connector is configured to send or store the JSON output.
- You may need to modify or expand the script to cover other segments (e.g., visit information) depending on your integration requirements.
Would you like an example covering additional HL7 segments or a different FHIR resource?
ChatGPT 4o answer 2024
Then we asked more difficult question to ChatGPT:
Question: Can you create a transformation step for converting an HL7 v2.4 ADT_A01 message to FHIR R4 in the Mirth Connect integration engine?
And here it is the provided answer:
Certainly! Transforming an HL7 v2.4 ADT_A01 message to a FHIR R4 Patient resource in Mirth Connect requires familiarity with both HL7v2 and FHIR message structures. Below, I’ll guide you through the steps to set up a transformation channel in Mirth Connect to map an ADT_A01 message to a FHIR R4 Patient resource.
Prerequisites
- Mirth Connect installed and configured.
- Knowledge of the structure of HL7 v2.4 ADT_A01 messages.
- Understanding of the FHIR R4 Patient resource JSON structure.
Step 1: Set up the Mirth Connect Channel
- Create a New Channel in Mirth Connect and name it something like ADT to FHIR Transformation.
- Set the Source Connector to accept HL7v2 messages. This could be configured as a TCP Listener, File Reader, or other suitable source connector depending on your input source.
- Set the Destination Connector to output JSON (FHIR format). You may want to use a REST API, File Writer, or Database Writer, depending on the destination requirements.
Step 2: Configure the Source Transformer
- Go to the Source Transformer in your Mirth channel and add a new step.
- Choose JavaScript as the type for your transformation step.
Step 3: Write JavaScript for Transformation
Add the following JavaScript code to transform the HL7 ADT_A01 message to FHIR R4 Patient JSON:
// Initialize the FHIR Patient resource
var patientResource = {
resourceType: "Patient",
id: $('MSH-10'), // Unique identifier from MSH segment
identifier: [
{
system: "urn:oid:2.16.840.1.113883.4.1", // Example for MRN system OID
value: $('PID-3.1') // MRN from PID segment
}
],
active: true,
name: [
{
use: "official",
family: $('PID-5.1'), // Last Name
given: [$('PID-5.2')] // First Name
}
],
telecom: [
{
system: "phone",
value: $('PID-13.1'), // Phone Number
use: "home"
},
{
system: "email",
value: $('PID-13.4'), // Email if available
use: "home"
}
],
gender: $('PID-8') === 'M' ? 'male' : 'female', // Gender mapping
birthDate: DateUtil.formatDate($('PID-7'), 'yyyyMMdd', 'yyyy-MM-dd'), // DOB with format change
address: [
{
use: "home",
line: [$('PID-11.1')], // Address line
city: $('PID-11.3'), // City
state: $('PID-11.4'), // State
postalCode: $('PID-11.5') // ZIP/Postal Code
}
]
};
// Add managing organization if available
if ($('PV1-3.1')) {
patientResource.managingOrganization = {
reference: "Organization/" + $('PV1-3.1') // Organization reference
};
}
// Convert the JavaScript object to JSON
var fhirPatientJson = JSON.stringify(patientResource, null, 2);
// Map the JSON output to the destination
msg = fhirPatientJson;
Explanation of the Transformation Code
- Resource Initialization: The patientResource object is initialized with the necessary FHIR R4 Patient resource fields.
- Identifier: The identifier is mapped from PID-3.1 (MRN), with a sample OID for the system.
- Name: The name fields use PID-5 for last and first names.
- Telecom: Phone and email details are mapped from PID-13.
- Gender: The gender field maps ‘M’ and ‘F’ to ‘male’ and ‘female’, as per FHIR requirements.
- Birth Date: Formats the birth date from yyyyMMdd (HL7) to yyyy-MM-dd (FHIR).
- Address: Maps address components from PID-11.
- Managing Organization: Maps organization information if present from PV1-3.
Step 4: Configure the Destination Connector
- Go to the Destination Connector and set the data type to JSON if you’re outputting JSON directly.
- Use msg as the output variable, as it contains the JSON representation of the FHIR Patient resource.
- Configure the destination endpoint (such as REST API URL or file location) to send the transformed JSON message to your FHIR-compliant system.
Step 5: Test the Channel
- Deploy the channel and send a test ADT_A01 HL7 message to verify the transformation.
- Check the output in your configured destination to ensure that the JSON format conforms to FHIR R4 standards.
Additional Notes
- Testing and Validation: Use FHIR validators or online tools to ensure the output JSON complies with FHIR R4 standards.
- Enhancements: You may want to add error handling and additional mappings depending on specific fields in your ADT message or FHIR implementation requirements.
This setup will transform an incoming HL7 v2.4 ADT_A01 message into a FHIR R4 Patient resource, ready to be consumed by a FHIR-compliant system.
Are you happy with the ChatGPT answer?
As you can see, the answer is currently not complete and requires additional manual intervention from an expert engineer.
However, ChatGPT never stops evolving, just like us.
During this time, we’ve developed our own breakthrough interoperability platform — Meditecs Smart Connect powered by human intelligence by the way 😀.
This platform is already helping our clients like Abbott, NHC, and Boston Scientific effortlessly integrate new medical devices and groundbreaking healthcare solutions, while cutting integration and support costs by up to 40%.
And what tools do you use to accelerate integration? Do you use GenAI to speed up interoperability engineering tasks? Share it in the comments.




Thank you, It is quite interesting! I use Mirth but haven’t tried Chat GPT to convert HL7 messages to FHIR yet. Nice to know it can be used to simplify some tedious and repetitive tasks. Thanks for sharing!
Thanks Chris, we are currently experimenting with this and encourage you to test it with us. Would you be willing to share the results of your experiments with our community?
Thanks for the great article, but even ChatGPT4o’s response is not good at all. It explains a bit about the steps that need to be done in Mirth before the code, but some of the code is incorrect. This makes sense since there isn’t a lot of public information on how to do this. And some existing tables mapping HL7 V2 messages to FHIR are so complex that AI can not interpret them. Perhaps with a more detailed prompt you can get something better. But now it’s 1:0 in favor of the engineers I would say! 😉
Javier, thanks for the feedback. This shows how AI can support, but not replace engineers in healthcare integration. We’ve found that tools like GPT can be great for speeding up the initial stages of coding, but fine-tuning and testing are still critical at this stage. To streamline integration, you can use Meditecs Smart Connect, which provides dozens of automated workflows and advanced features.
This is a fascinating experiment! That said, I agree that expert engineers are still essential for ensuring quality and accuracy, but Gen AI can free up our hands. I would love to give it a try, thanks for the inspiration!
Thanks Nick, we’re glad to hear that Meditecs has inspired you to optimize your interoperability tasks using Gen AI. We’d love to hear how it goes, and we look forward to hearing about your experiments on our forum.
AI in interoperability is definitely a step forward! Does your platform already use AI assistance? Could you share more examples of how it’s been implemented in real-world integrations? Thanks.
Absolutely, Alex, thanks for the question. We’re excited to share more use cases in upcoming news. Stay tuned, that’s coming soon.