How to split OBX segments of variable length separated by "`" and map to individual JSON fields using Javascript

I am mapping HL7 messages into JSON and came a across a scenario that I am unsure of.

I need to convert OBX segments, like the below:

OBX|1|CE|2643^MED LIST&PRESCRIBED MEDICATIONS^||VYVANSE`RITALIN`CONCERTA`VIVITROL`BRIXADI`SUBLOCADE

And the individual entries in the OBX.5.1 need to be mapped to [‘order’][‘drugs’][‘drugName’] like below:

"drugs" : [
    {
      "drugName" : "VYVANSE"
    },
    {
      "drugName" : "RITALIN"
    },
    {
      "drugName" : "CONCERTA"
    },        
    {
      "drugName" : "VIVITROL"
    },
    {
      "drugName" : "BRIXADI"
    },
    {
      "drugName" : "SUBLOCADE"
    },
],

I usually use the below, but it only maps the whole segment and not the individual back ticked entries in the segment.

var drg = -1;
for each (seg in msg..OBX)
    {
        var obx2_1 = seg['OBX.2']['OBX.2.1'].toString();
    if ( obx2_1.match(/CE/) )
        {
            if(seg['OBX.5']['OBX.5.1'].toString() != "NONE")
                {
                    drg++;
                    tmp['order']['drugs'][drg] = {};
                    var drug = tmp['order']['drugs'][drg];
                    drug['drugName'] = seg['OBX.5']['OBX.5.1'].toString();
                }
        };
        if(obx2_1.toString()=="SpecInstruc"){
            tmp['order']['comments'] = seg['OBX.5']['OBX.5.1'].toString();
        }
        
    };

I’m unsure what the best way to code this as the number of drugs in the OBX.5.1 may be variable as well as the total OBXs themselves, like so:

OBX|1|CE|2643^MED LIST&PRESCRIBED MEDICATIONS^||VYVANSE`RITALIN`CONCERTA`VIVITROL`BRIXADI`SUBLOCADE
OBX|1|CE|2641^MED LIST&PRESCRIBED MEDICATIONS^||AMBIEN`XANAX

To handle the scenario where the OBX.5.1 field contains multiple entries separated by backticks (```), you can split the field content into individual drugs and map them accordingly. Below is an updated approach to achieve your desired JSON structure:

Revised Code

var drg = -1;
for each (seg in msg..OBX) {
    // Check if OBX.2.1 equals "CE"
    var obx2_1 = seg['OBX.2']['OBX.2.1'].toString();
    if (obx2_1.match(/CE/)) {
        // Get the OBX.5.1 value and split it by backticks
        var drugList = seg['OBX.5']['OBX.5.1'].toString();
        if (drugList != "NONE") {
            var drugs = drugList.split('`'); // Split by backtick to get individual drugs
            for each (drugName in drugs) {
                drg++;
                tmp['order']['drugs'][drg] = {};
                var drug = tmp['order']['drugs'][drg];
                drug['drugName'] = drugName.trim(); // Trim to remove extra spaces
            }
        }
    }

    // Handle "SpecInstruc" case (if needed)
    if (obx2_1.toString() == "SpecInstruc") {
        tmp['order']['comments'] = seg['OBX.5']['OBX.5.1'].toString();
    }
}

Explanation

  1. Extract OBX.2.1:
  • Use seg['OBX.2']['OBX.2.1'].toString() to check if the segment type is CE.
  1. Split OBX.5.1:
  • Use split('')` to break the string into individual drug names based on the backtick delimiter.
  1. Iterate Over Drugs:
  • Loop through the resulting array of drug names and add each one as a new entry in the order['drugs'] array.
  1. Trim Drug Names:
  • Use trim() to ensure there are no leading or trailing spaces in the drug names.
  1. Variable Number of Drugs and OBX Segments:
  • The split function dynamically handles any number of drugs in OBX.5.1.
  • The outer loop processes all OBX segments, allowing for multiple segments with drug lists.

Input Example

OBX|1|CE|2643^MED LIST&PRESCRIBED MEDICATIONS^||VYVANSE`RITALIN`CONCERTA`VIVITROL`BRIXADI`SUBLOCADE
OBX|2|CE|2641^MED LIST&PRESCRIBED MEDICATIONS^||AMBIEN`XANAX

Output JSON

{
  "order": {
    "drugs": [
      {
        "drugName": "VYVANSE"
      },
      {
        "drugName": "RITALIN"
      },
      {
        "drugName": "CONCERTA"
      },
      {
        "drugName": "VIVITROL"
      },
      {
        "drugName": "BRIXADI"
      },
      {
        "drugName": "SUBLOCADE"
      },
      {
        "drugName": "AMBIEN"
      },
      {
        "drugName": "XANAX"
      }
    ]
  }
}

Notes

  • The code is robust enough to handle varying numbers of drugs in OBX.5.1 and multiple OBX segments.
  • If OBX.5.1 contains NONE, it skips the processing for that segment.
  • This approach can be easily extended to handle other fields or additional logic.