Skip to content

parser / 02

Quick start

Read a 271 response from a file and print each subscriber with the number of benefits that the payer reported.

Run it ​

Save a 271 response as response.x12, or download the single benefit example and rename it. Then run the following code:

quick-start.ts
ts
import { readFile } from 'node:fs/promises';

import { readX12 } from '@ediocracy/parser';
import { read271s } from '@ediocracy/parser/271';

const text = await readFile('response.x12', 'utf8');

for (const { response } of read271s(readX12(text))) {
    for (const subscriber of response.subscribers) {
        const lastName = subscriber.name?.element(3);
        const firstName = subscriber.name?.element(4);
        console.log(`${firstName} ${lastName}: ${subscriber.benefits.length} benefits`);
    }
}
outputJORDAN REED: 1 benefits

What the code does ​

Select a step to highlight its lines.

  1. The readX12 function splits the text into interchanges, functional groups, and transaction sets. If the text isn't X12, it throws an X12SyntaxError error.
  2. The read271s function reads every 271 transaction set in the document and skips any other transaction set.
  3. Each subscriber's name property is the NM1 segment that names the subscriber. element(3) returns the last name (NM103) and element(4) returns the first name (NM104).

Next steps ​