ASCII X12 EDI Library, Create, Parse, Load, Validate, View, Edit, Split, Join And Scrub EDI Data
npm install rdpcrystal-edi-librarybash
npm i rdpcrystal-edi-library
`
Usage
Parse an 5010 837I X12 EDI File
`javascript
///
const edi = require('../../lib/RDPCrystalEDILibrary').RDPCrystalEDILibrary;
const fs = require('fs');
const enumMap = require('./enummap');
let map = new enumMap();
//Create a new validator
let validator = new edi.EDIValidator();
//Load a 5010 270 validation rules
let validationRules = fs.readFileSync('../../validationrules/5010/Rules_5010_837I_005010X223A2.Rules').toString();
validator.EDIRulesFileData = validationRules;
//Set the EDI data to validate and load. This can also be read in from a file
//Set EDI data to validate and load
validator.EDIDataString = "ISA00 00 ZZ87726 ZZ593697504 1208150801^005010000000010T:~" +
"GSHC87726BOAFHC201208150801591X005010X223A2~"+
"ST837000000001*005010X223A2~"+
"BHT00190000000000120120815080159CH~"+
"NM1412UNITED HEALTH46*12345~"+
"PERICTOM JONESTE1234567891~"+
"NM1402UNITED HEALTH46*87726~"+
"HL1201~"+
"NM1852HOSPITAL INCXX*12345~"+
"N3*PO BOX 633571~"+
"N4CINCINNATIOH*452633571~"+
"REFEI12345~"+
"HL21220~"+
"SBRP1812345GE*13~"+
"NM1IL1VAZQUEWSMITH*MI123456~"+
"N3*1234 LAKE~"+
"N4FAIRFIELDOH*12345~"+
"DMGD819520206*M~"+
"NM1PR2BANK OF AMERICAPI*123456~"+
"CLM100000148939002017.8522:A:1AY*Y~"+
"DTP434RD8*20120723-20120723~"+
"CL117*30~"+
"REFD9123456~"+
"HI*BK:7244~"+
"HIBF:4019BF:2720BF:25000BF:2749BF:V4582BF:7244~"+
"NM1711ORLANDOSING~"+
"LX*1~"+
"SV2250HC:OPS2017.85UN1*0~"+
"DTP47xx2RD8*20120723-20120723~"+
"SVD877261378.4HC:OPS00~"+
"CASPR1002344.60300~"+
"DTP573D8*20120814~"+
"HL3201~"+
"NM1852HOSPITAL INCXX*123456~"+
"N3*PO BOX 633571~"+
"N4CINCINNATIOH*452633571~"+
"REFEI123456~"+
"HL43221~"+
"SBRP18304000GE*13~"+
"NM1IL1GatesBill*MI12345~"+
"N3*999 EASTWICK~"+
"N4CINCINNATINY*12345~"+
"DMGD819440928*M~"+
"NM1PR2BANKPI*12345~"+
"HL54230~"+
"PAT*01~"+
"NM1QC1GatesBill~"+
"N3*999 EASTWICK~"+
"N4CINCINNATINY*12345~"+
"DMGD819540531*F~"+
"CLM100000155671001820.3222:A:1AY*Y~"+
"DTP434RD8*20120727-20120727~"+
"CL117*30~"+
"REFD912345~"+
"HI*BK:V7651~"+
"HIBF:4550BF:4019BF:V1272BF:V7651~"+
"NM1711MCDONALDRONALD~"+
"LX*1~"+
"SV2250HC:OPS1820.32UN4*0~"+
"DTP472RD8*20120727-20120727~"+
"SVD877261348.62HC:OPS00~"+
"CASPR1134.5502337.150300~"+
"DTP573D8*20120814~"+
"SE60000000001~"+
"GE11~"+
"IEA1000000001~";
console.log("Validating 5010 837I EDI File");
validator.validate();
//Get the EDI document loaded into memory
let loadedEDIFile = validator.EDILightWeightDocument;
//Get EDI Data
let stTransaction = loadedEDIFile.Loops.getItem(0).getLoop("INTERCHANGE HEADER/FUNCTIONAL GROUP/ST HEADER")
let stSegment = stTransaction.getSegment("ST");
let ediData = "";
ediData += "Transaction Name = " + stSegment.Name + "\n";
ediData += "Transaction Number = " + stSegment.Elements.getItem(1).DataValue + "\n";
ediData += "Transaction Impl = " + stSegment.Elements.getItem(2).DataValue + "\n";
ediData += "\n";
//Get all billing providers
let billingProviderLoops = stTransaction.getLoops("2000A");
for(let i=0;i < billingProviderLoops.Count;i++)
{
//Get billing provider name
let billingProviderLoop = billingProviderLoops.getItem(i).getLoop("2010AA");
let billingProviderNameSegment = billingProviderLoop.getSegment("NM1");
ediData += "Billing Provider = " + billingProviderNameSegment.Elements.getItem(2).DataValue+ "\n";
//Get subscriber name
let subscriberLoop = billingProviderLoops.getItem(i).getLoop("2000B/2010BA");
let subscriberNameSegment = subscriberLoop.getSegment("NM1");
ediData += "Subscriber Name = " + subscriberNameSegment.Elements.getItem(2).DataValue+ "\n";
//Get the claim loop
let claimLoop = billingProviderLoops.getItem(i).getLoop("2000B/2300");
if (claimLoop == null){
claimLoop = billingProviderLoops.getItem(i).getLoop("2000B/2000C/2300");
}
let claimSegment = claimLoop.getSegment("CLM");
ediData += "Claim ID = " + claimSegment.Elements.getItem(0).DataValue + "\n";
ediData += "Claim Price = " + claimSegment.Elements.getItem(1).DataValue + "\n";
let serviceLoops = claimLoop.getLoops("2400");
for(let j=0;j < serviceLoops.Count;j++)
{
let serviceSegment = serviceLoops.getItem(j).getSegment("SV2");
ediData += "Service Price= " + serviceSegment.Elements.getItem(2).DataValue+ "\n";
}
ediData += "\n\n";
}
console.log(ediData);
`
Validate an X12 EDI File
`javascript
const fs = require('fs');
const edi = require('rdpcrystal-edi-library');
//Create a new validator
let validator = new edi.EDIValidator();
//Load a 5010 270 validation rules
let validationRules = fs.readFileSync('Rules_5010_270_005010X279A1.Rules').toString();
validator.EDIRulesFileData = validationRules;
//Set the EDI data to validate and load. This can also be read in from a file
validator.EDIDataString = "ISA00..........01SECRET....ZZSUBMITTERS.ID..ZZRECEIVERS.ID...0301011253^005010000009051T:~" +
"GSHkSENDER CODERECEIVERCODE1999123108021X005010X279A1~" +
"ST2711234*005010X279A1~" +
"BHT0022131000123420060501*1319~" +
"HL1201~" +
"NM1PR2ABC COMPANYPI*842610001~" +
"HL21211~" +
"NM11P2BONE AND JOINT CLINICSV*2000035~" +
"HL32220~" +
"TRN193175-012547*9877281234~" +
"NM1IL1SMITHROBERT*MI11122333301~" +
"DMGD819430519~" +
"DTP291D8*20060501~" +
"EQ*30~" +
"SE131234~" +
"GE11~" +
"IEA1000000905~";
console.log("Validating 5010 271 EDI Files");
validator.validate();
console.log("Errors Found");
//Get all errors from the EDI data
for (let i = 0; i < validator.Errors.Count; i++) {
let error = validator.Errors.getItem(i);
console.log(
{
Type: "Error",
Line: error.LineNumber,
Transaction: "",
SnipLevel: error.SnipLevel, //SnipTestLevel Enum
Message: error.Message, //EDIValidationMessage Enum
Loop: error.Loop,
Segment: error.Segment,
Element: error.ElementOrdinal,
Composite: error.CompositeElementOrdinal,
Description: error.Description,
Ordinal: error.SegmentOrdinal
});
}
//Write out a visual representation of the loaded EDI document
let doc = validator.EDILightWeightDocument;
writeDocumentTree(doc.Loops.getItem(0), 0);
function writeDocumentTree(loop, indent) {
if (loop != null) {
for (let i = 0; i < indent; i++) {
process.stdout.write(" ");
}
console.log(loop.Name);
indent++;
writeSegment(loop.Segments, indent);
if (loop.Loops != null) {
for (let i = 0; i < loop.Loops.Count; i++) {
writeDocumentTree(loop.Loops.getItem(i), indent);
}
indent++;
}
}
}
function writeSegment(segments, indent) {
for (let i = 0; i < segments.Count; i++) {
let seg = segments.getItem(i);
for (let i = 0; i < indent; i++) {
process.stdout.write(" ");
}
console.log(seg.Name);
writeElement(seg.Elements, indent);
}
}
function writeElement(elements, indent) {
for (let i = 0; i < elements.Count; i++) {
let elem = elements.getItem(i);
for (let i = 0; i < indent; i++) {
process.stdout.write(" ");
}
if (elem.Composite) {
console.log("Composite");
//Check for composite elements
if (elem.Elements != null && elem.Elements.Count > 0) {
writeElement(elem.Elements, ++indent);
indent--;
}
} else {
console.log("[" + elem.DataValue + "]");
}
}
}
`
Create An X12 EDI File
`javascript
const edi = require('rdpcrystal-edi-library');
const STAR = 42; //'*'
const COLON = 58 //':'
const TILDA = 126 //'~'
let doc = new edi.EDILightWeightDocument();
//Set the delimiters
doc.Delimiters.ElementTerminatorCharacter = STAR;
doc.Delimiters.CompositeTerminatorCharacter = COLON;
doc.Delimiters.SegmentTerminatorCharacter = TILDA;
//Automatically set current segment count in SE01
doc.AutoPlaceCorrectNumOfSegments = true;
//Write each segment in a new line
doc.EachSegmentInNewLine = true;
let interchangeLoop = doc.createLoop("Interchange header");
let isa = interchangeLoop.createSegment("ISA");
isa.addElement("00");
isa.addElement(" ");
isa.addElement("00");
isa.addElement(" ");
isa.addElement("ZZ");
isa.addElement("InterchangeSenderID");
isa.addElement("ZZ");
isa.addElement("InterchangeReceiverID");
isa.addElement("070303");
isa.addElement("1804");
isa.addElement("U");
isa.addElement("00401");
isa.addElement("1");
isa.addElement("1");
isa.addElement("T");
isa.addElement(":");
let functionalGroup = interchangeLoop.createLoop("FunctionalGroup");
let gs = functionalGroup.createSegment("GS");
gs.addElement("T");
gs.addElement("SH");
gs.addElement("ApplicationSenderCode");
gs.addElement("ApplicationReceiverCode");
gs.addElement("2005");
gs.addElement("132334");
gs.addElement("1");
gs.addElement("X");
gs.addElement("004010");
//Create a new sample composite element
let compositeElement = new edi.LightWeightElement();
compositeElement.addCompositeElement("aa");
compositeElement.addCompositeElement("bb");
gs.elements.add(compositeElement);
let transaction = functionalGroup.createLoop("Transaction Header");
let st = transaction.createSegment("ST");
st.addElement("837");
st.addElement("123");
st.addElement("005010X222A1");
let se = transaction.createSegment("SE");
se.addElement("0");
se.addElement("123");
let endfunctionalGroup = functionalGroup.createLoop("EndFunctionalGroup");
let ge = endfunctionalGroup.createSegment("GE");
ge.addElement("0");
ge.addElement("1");
let endInterchange = interchangeLoop.createLoop("EndInterchange");
let iea = endInterchange.createSegment("IEA");
iea.addElement("0");
iea.addElement("1");
//Display the new EDI document
console.log(doc.generateEDIData());
`
Generate X12 EDI 999 Acknowlegements
`javascript
const fs = require('fs');
const edi = require('rdpcrystal-edi-library');
//Create a new validator
let validator = new edi.EDIValidator();
//Load a 5010 270 validation rules
let validationRules = fs.readFileSync('Rules_5010_270_005010X279A1.Rules').toString();
validator.EDIRulesFileData = validationRules;
//Set the EDI data to validate and load. This can also be read in from a file
validator.EDIDataString = "ISA00..........01SECRET....ZZSUBMITTERS.ID..ZZRECEIVERS.ID...0301011253^005010000009051T:~" +
"GSHkSENDER CODERECEIVERCODE1999123108021X005010X279A1~" +
"ST2711234*005010X279A1~" +
"BHT0022131000123420060501*1319~" +
"HL1201~" +
"NM1PR2ABC COMPANYPI*842610001~" +
"HL21211~" +
"NM11P2BONE AND JOINT CLINICSV*2000035~" +
"HL32220~" +
"TRN193175-012547*9877281234~" +
"NM1IL1SMITHROBERT*MI11122333301~" +
"DMGD819430519~" +
"DTP291D8*20060501~" +
"EQ*30~" +
"SE131234~" +
"GE11~" +
"IEA1000000905~";
console.log("Validating 5010 271 EDI Files");
validator.validate();
console.log("Errors Found->");
//Get all errors from the EDI data
for (let i = 0; i < validator.Errors.Count; i++) {
let error = validator.Errors.getItem(i);
console.log(
{
Type: "Error",
Line: error.LineNumber,
Transaction: "",
SnipLevel: error.SnipLevel,
Message: error.Message,
Loop: error.Loop,
Segment: error.Segment,
Element: error.ElementOrdinal,
Composite: error.CompositeElementOrdinal,
Description: error.Description,
Ordinal: error.SegmentOrdinal
});
}
console.log("Generated 5010 999 Acknowledgment File");
//Generate 5010 999 Acknowledgment File
let ack999Generator = new edi.Ack999Generator();
let ackDocument = ack999Generator.generate(validator);
console.log(ackDocument.generateEDIData());
`
Spit an X12 EDI File
`javascript
const edi = require('rdpcrystal-edi-library');
//original document with 2 Transactions (ST) -
//PLEASE ADD YOUR OWN EDI DATA HERE
let original ="ISA00 00 ZZ133052274 ZZ311279999 ...";
let splitter = new edi.EDIFileSplitter();
//Split the document at the ST header
splitter.FileSplitLevel = edi.FileSplitLevel.HEADER;
//Put 1 ST-SE loop in each file
splitter.NumberOfItemsPerFile = 1;
//split the orginal document
let splitDocs = splitter.split(original);
console.log("First Split Document");
console.log(splitDocs[0]);
console.log("Second Split Document");
console.log(splitDocs[1]);
`
Join an EDI File
`javascript
const edi = require('rdpcrystal-edi-library');
//First document
//PLEASE ADD YOUR OWN EDI DATA HERE
let ediDoc1 = "ISA00 00 ZZ133052274 ZZ311279999...";
//Second Document
//PLEASE ADD YOUR OWN EDI DATA HERE
let ediDoc2 = "ISA00 00 ZZ133052274 ZZ311279999...";
let documents = [ediDoc1, ediDoc2];
let joiner = new edi.EDIFileJoiner();
//This these two documents at the functional group (GS) level
joiner.FileJoinLevel = edi.FileJoinLevel.FUNCTIONALGROUP;
//Join the two documents
let joinedDocuments = joiner.join(documents);
console.log("Joined document by Functional Group");
console.log(joinedDocuments);
`
Load an X12 EDI File
`javascript
const edi = require('rdpcrystal-edi-library');
//PLEASE ADD YOUR OWN EDI DATA HERE
let dataToLoad = "ISA00 00 ZZ133052274 ZZ311279999...";
let fileLoader = new edi.EDIFileLoader();
//Load EDI data
fileLoader.EDIDataString = dataToLoad;
let flatDoc = fileLoader.load();
//Build a visual representation of the EDI document
writeDocumentTree(flatDoc.Loops.getItem(0), 0);
function writeDocumentTree(loop, indent) {
if (loop != null) {
for (let i = 0; i < indent; i++) {
process.stdout.write(" ");
}
console.log(loop.Name);
indent++;
writeSegment(loop.Segments, indent);
if (loop.Loops != null) {
for (let i = 0; i < loop.Loops.Count; i++) {
writeDocumentTree(loop.Loops.getItem(i), indent);
}
indent++;
}
}
}
function writeSegment(segments, indent) {
for (let i = 0; i < segments.Count; i++) {
let seg = segments.getItem(i);
for (let i = 0; i < indent; i++) {
process.stdout.write(" ");
}
console.log(seg.Name);
writeElement(seg.Elements, indent);
}
}
function writeElement(elements, indent) {
for (let i = 0; i < elements.Count; i++) {
let elem = elements.getItem(i);
for (let i = 0; i < indent; i++) {
process.stdout.write(" ");
}
if (elem.Composite) {
console.log("Composite");
//Check for composite elements
if (elem.Elements != null && elem.Elements.Count > 0) {
writeElement(elem.Elements, ++indent);
indent--;
}
} else {
console.log("[" + elem.DataValue + "]");
}
}
}
``