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:
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.
- The
readX12function splits the text into interchanges, functional groups, and transaction sets. If the text isn't X12, it throws anX12SyntaxErrorerror. - The
read271sfunction reads every 271 transaction set in the document and skips any other transaction set. - Each subscriber's
nameproperty is theNM1segment that names the subscriber.element(3)returns the last name (NM103) andelement(4)returns the first name (NM104).
Next steps
Describes the fields of a response.
Describes what happens with malformed input.