Fixing line breaks in HL7 messages in Mirth Connect

Anyone who has worked in the world of healthcare integration or with HL7 knows that if you have seen one HL7 message, you’ve seen one HL7 message.  Now, a common problem with some source systems is that a line break will sneak in the middle of a segment, rendering the whole message invalid.  How many times have you seen this message?

MSH|^~\&|DDTEK LAB|ELAB-1|DDTEK OE|BLDG14|200502150930||ORU^R01^ORU_R01|CTRL-9876|P|2.4
PID|||010-11-1111||Estherhaus^Eva^E^^^^L|Smith|19720520|F|||256 Sherwood Forest Dr.^^Baton Rouge^LA^70809||(225)334-5232|(225)752-1213||||AC010111111||76-B4335^LA^20070520
OBR|1|948642^DDTEK OE|917363^DDTEK LAB|1554-5^GLUCOSE|||200502150730|||||||||020-22-2222^Levin-Epstein^Anna^^^^MD^^Micro-Managed
Health Associates|||||||||F|||||||030-33-3333&Honeywell&Carson&&&&MD
OBX|1|SN|1554-5^GLUCOSE^^^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^175|mg/dl|70_105|H|||F

Notice the “Health Associates” segment? Obviously, this segment invalidates the message.  When this message is sent through a Mirth channel that expects incoming HL7, the message will error out when Mirth tries to parse it.  Here is how we can fix that.

The Solution

To fix this, we’re going to use a channel’s preprocessor script to append seemingly broken segments to the previous segment.  Since the preprocessor script is invoked before Mirth’s HL7 parser, we don’t have to worry about Mirth’s HL7 parser choking on it.

Instead of explaining the logic in a separate paragraph, I have left inline comments to explain what is happening.  I think, though, that it is fairly easy to follow.

Paste the following code into your channel’s preprocessor script.

The Code

// initialize a first_iteration flag.
// we don't want to add a carriage return
// before the MSH segment
var first_iteration = true;

// initialize the new message as an empty string
var new_message = '';

// loop over each line in the message
// by splitting on the hl7 standard carriage return
for each (var line in message.split(/\r/)) {

  // if it is a valid hl7 segment, we'll
  // append the new line to the new message
  if(line.match(/^[a-zA-Z0-9]{3}\|/)) {
    // see the first comment
    if( ! first_iteration){
      new_message += '\r';
    new_message += line;
  } else {
    // if it isn't a valid hl7 segment,
    // we'll append it to the end of the
    // previous segment instead
    new_message += ' ' + line;

  // it will never be the first_iteration again
  first_iteration = false;

//send the fixed message to the channel
message = new_message;

return message;
Leave a comment ?


  1. Nice, it works perfectly for me!

  2. Just as friendly suggestion, to those readers who are not quite familiar with the Mirth Connect, as a starting point they may try the “Unofficial Mirth Connect developer’s guide” book to get some hands-on experience.

    (Disclaimer: I’m the author of this book, so any comments or suggestions are welcome.)

  3. This worked great for me, though I did need to make two small changes.

    1. Instead of splitting on \r, I had to split on \r\n.
    2. Instead of appending \r to the beginning of a valid line, I had to append \r\n.

    Other than those two items, this worked perfectly for me.

  4. 1% of my channel messages errored out due to line break.

    I used the above code (split on \r\n and appending \r\n)

    It works on first message, but don’t work on second messages. Any ideas why?

    NTE|||according to GP note
    ? due to alcohol component

    OBX||CE|||Patient is enrolled into VISION study^^HA|||||||||20140929111901
    NTE|||Observation study, duration 18 mths
    PI : Dr HELLO WORLD, co-ordinator: Ms Tracy at 2632 3663
    CRE 2014.136-T

  5. Thanks for posting

    I change to code to the following in the pre-processor scripts:

    message = message.replace(/[\r\n]+(?![A-Z][A-Z][A-Z0-9]\|)/g, ” “);

    (got from Mirth Forum)

    It now works

  6. How does the code

    // if it is a valid hl7 segment, we’ll
    // append the new line to the new message
    if(line.match(/^[a-zA-Z0-9]{3}\|/)) {

    determine it’s a valid HL7 segment?

    • It makes sure the first 3 characters are either letters or numbers and are followed by a “|”. It doesn’t check the actual value against the HL7 spec, but that can be handled later. This script’s objective is to just get the file serialized into Mirth without errors so it can be processed.

      i.e. OBX| or GT1| or 123| etc…

  7. I need line break for each segment when saving the messages into a Text file. The given code is not working. Can you help

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>