Parsing EDI Segments and Elements (All Transactions)

Our EDI Java parser offers an API for working directly with X12 EDI segments and elements. This API can be used as an alternative to the object model API. It can also be used for EDI transactions where the object model is not offered, e.g., 834 or 271 transaction types.

The parser returns a list of EDI segments. A segment can contain child segments, depending on the loop hierarchy. E.g., a CLM segment contains a list of SV1 (service line) segments.

The parser offers several ways to search for segments by their types (ID), names, and other parameters, e.g.:

EdiParsingResults results = parser.parse();
List<EdiSeg> tranSegs = results.findSegsByType(SegmentType.ST);
EdiSeg tran = tranSegs.get(0);

List<EdiSeg> tranChildSegs = tran.childSegs();
var segMatcher = new SegMatcher();
// find claims for this transaction
var claimSegs = segMatcher.findByType(tranChildSegs, SegmentType.CLM);
assertThat(claimSegs).hasSize(1);
// get the first claim
var claimSeg = claimSegs.get(0);

A segment contains the list of Elt objects representing EDI elements. Elt contains the element’s name, value, data type, and other metadata.

Here is an example:

EdiParsingResults results = parser.parse();
var claimSeg = results.findFirstSegByType(SegmentType.CLM);
assertThat(claimSeg).isNotNull();

// iterate over elts and do something with them based on name or position (CLM01, CLM05, etc)
// position is defined by the spec, elements could be omitted. for CLM, positions are 1, 2, 5, 7, 8,9
String patientAccountNumber=null;
BigDecimal chargeAmount=null;
String medicareAssessmentCode = null;
for (Elt elt : claimSeg.eltSet().elts()) {

    switch (elt.position()) {
        // CLM01
        case 1 -> patientAccountNumber=elt.stringVal();
        // CLM02, charge amount
        case 2 -> chargeAmount=elt.bigDecimalVal();
        // CLM07, medicare assignment code
        case 7 -> medicareAssessmentCode = elt.stringVal();
    }
}
assertThat(patientAccountNumber).isEqualTo("26462967");
assertThat(chargeAmount).isEqualTo(new BigDecimal("100.00"));
assertThat(medicareAssessmentCode).isEqualTo("A");

// or by name
chargeAmount = claimSeg.eltByName("total_claim_charge_amount").bigDecimalVal();
assertThat(chargeAmount).isEqualTo(new BigDecimal("100.00"));

You can use toFormattedStringWithChildren() method on any segment to view an indented tree of all segments and their elements. This is useful for troubleshooting purposes or to quickly review the output of the parser:

EdiParsingResults results = parser.parse();
List<EdiSeg> claimSegs = results.findSegsByType(SegmentType.CLM);
EdiSeg claimSeg = claimSegs.get(0);
System.out.println(claimSeg.toFormattedStringWithChildren());
// or print all segments using the static method
System.out.println(EdiSeg.toFormattedStringWithChildren(claimSegs));

Here is the fragment of the output:

CLM*26462967*100***11:B:1*Y*A*Y*I
claim_information (Loop: 2300 Seg.ID: CLM IsLoopParent: true Index within tran:21  Parent: transaction_set_header)
  patient_account_number (String) (1) [26462967]: 26462967
  total_claim_charge_amount (Double) (2) [100]: 100.0
  health_care_service_location_information (EltSet) (5) [11:B:1]: 
    facility_type_code (String) (1) [11]: 11	Code Ent: 11 null, facility_code_qualifier (String) (2) [B]: B, claim_frequency_code (String) (3) [1]: 1	Code Ent: 1 null
  provider_or_supplier_signature_indicator (String) (6) [Y]: Y
  medicare_assignment_code (String) (7) [A]: A
  benefits_assignment_certification_indicator (String) (8) [Y]: Y
  release_of_information_code (String) (9) [I]: I

    DTP*431*D8*19981003
    date_onset_of_current_illness_symptom (Loop: 2300 Seg.ID: DTP IsLoopParent: false Index within tran:22  Parent: claim_information)
    Saturday, October 3, 1998
      date_time_qualifier (String) (1) [431]: 431
      date_time_period_format (DateFormatType) (2) [D8]: Date
      onset_of_current_illness_or_injury_date (LocalDate) (3) [19981003]: Saturday, October 3, 1998
    REF*D9*17312345600006351
    claim_identifier_for_transmission_intermediaries (Loop: 2300 Seg.ID: REF IsLoopParent: false Index within tran:23  Parent: claim_information)
    17312345600006351
      reference_identification_qualifier (ReferenceType) (1) [D9]: Claim Number
      clearinghouse_trace_number (String) (2) [17312345600006351]: 17312345600006351

Please visit our GitHub repo for more examples.

Register below to get access to the parser: