Skip to main content

A comprehensive Python package for parsing, transforming, and mapping X12 EDI files

Project description

PyEDI

A comprehensive Python package for parsing, transforming, and mapping X12 EDI files to various target schemas using JSONata expressions.

Features

  • Complete X12 Parsing: Parse any X12 EDI file to generic JSON using the robust pyx12 library
  • Structured Formatting: Transform generic JSON to a structured format that preserves X12 organization
  • Flexible Schema Mapping: Map structured JSON to any target schema using powerful JSONata expressions
  • Simple Pipeline API: Easy-to-use pipeline for complete EDI transformation
  • Batch Processing: Process multiple EDI files efficiently
  • Comprehensive Code Sets: Built-in EDI code descriptions and lookups
  • Extensible Architecture: Use individual components or the complete pipeline

Installation

From PyPI

pip install pyedi

From Source

git clone https://github.com/jaymd96/pyedi.git
cd pyedi
pip install -e .

With Optional Features

# Install with CLI support
pip install pyedi[cli]

# Install with development tools
pip install pyedi[dev]

Quick Start

Simple Usage

from pyedi import X12Pipeline

# Create pipeline
pipeline = X12Pipeline()

# Transform EDI file with mapping
result = pipeline.transform(
    edi_file="path/to/835.edi",
    mapping="path/to/mapping.json"
)

print(result)

Step-by-Step Processing

from pyedi import X12Parser, StructuredFormatter, SchemaMapper

# Step 1: Parse EDI to generic JSON
parser = X12Parser()
generic_json = parser.parse("path/to/835.edi")

# Step 2: Format to structured JSON
formatter = StructuredFormatter()
structured_json = formatter.format(generic_json)

# Step 3: Map to target schema
mapper = SchemaMapper(mapping_definition)
target_json = mapper.map(structured_json)

Core Components

X12Parser

Parses X12 EDI files into generic JSON format while preserving all contextual information.

from pyedi import X12Parser

parser = X12Parser()
generic_json = parser.parse("input.edi")

Features:

  • Handles all X12 transaction types
  • Preserves loop structure and hierarchy
  • Includes segment metadata and paths
  • Validates using pyx12 maps

StructuredFormatter

Transforms generic JSON into a structured format with meaningful field names and code descriptions.

from pyedi import StructuredFormatter

formatter = StructuredFormatter()
structured_json = formatter.format(generic_json, include_technical=True)

Features:

  • Transaction-specific formatting (835, 837, 834, etc.)
  • Human-readable code descriptions
  • Preserves X12 structure
  • Optional technical code inclusion

SchemaMapper

Maps structured JSON to target schemas using JSONata expressions.

from pyedi import SchemaMapper

mapper = SchemaMapper(mapping_definition)
target_json = mapper.map(structured_json)

Features:

  • Powerful JSONata expression support
  • Lookup table support
  • Conditional transformations
  • Complex field mappings

Creating Custom Mappings

Using MappingBuilder

from pyedi import MappingBuilder

# Create builder
builder = MappingBuilder("my_mapping", mapping_type="only_mapped")

# Add simple field mappings
builder.add_field_mapping("payment_id", "trace_information.check_or_eft_number")
builder.add_field_mapping("payment_date", "payment_information.payment_date")

# Add object mapping
builder.add_object_mapping("payer", {
    "name": "payer.name",
    "id": "payer.identification.value"
})

# Add calculated fields using JSONata
builder.add_field_mapping("total_claims", "$count(claims)")

# Build and export
mapping = builder.build()
builder.export_to_file("my_mapping.json")

Inline Mapping Definition

mapping = {
    "name": "simple_835_extract",
    "mapping_type": "only_mapped",
    "expressions": {
        "payment_id": "trace_information.check_or_eft_number",
        "payment_amount": "payment_information.total_payment_amount",
        "claim_count": "$count(claims)",
        "claims": "claims ~> |$| { 'id': patient_control_number, 'amount': total_paid_amount } |"
    }
}

Mapping Types

  • only_mapped: Output contains only mapped fields
  • merge_with_target: Start with target template, override with mapped values
  • pass_through: Start with source data, override with mapped values

JSONata Expression Examples

Basic Field Access

"payment_information.payment_date"  // Access nested field
"claims[0].claim_number"            // Array index access

Calculations

"$count(claims)"                    // Count items
"$sum(claims.total_paid_amount)"    // Sum values

Transformations

// Transform array of objects
"claims ~> |$| { 'id': patient_control_number, 'amount': total_paid_amount } |"

// Filter and transform
"claims[total_charge_amount > 1000] ~> |$| claim_number |"

Conditionals

"claim_status.code = '1' ? 'Paid' : 'Denied'"

Batch Processing

pipeline = X12Pipeline(verbose=True)

results = pipeline.transform_batch(
    edi_files=["file1.edi", "file2.edi", "file3.edi"],
    mapping="mapping.json",
    output_dir="output/"
)

print(f"Processed: {results['statistics']['files_processed']}")
print(f"Succeeded: {results['statistics']['files_succeeded']}")

CLI Usage

After installation with CLI support:

# Transform single file
x12-convert input.edi --mapping mapping.json --output result.json

# Transform with options
x12-convert input.edi --mapping mapping.json --verbose --save-intermediate

# Batch processing
x12-convert *.edi --mapping mapping.json --batch --output-dir results/

Supported Transaction Types

  • 835 - Healthcare Claim Payment/Remittance Advice
  • 837 - Healthcare Claim (Professional, Institutional, Dental)
  • 834 - Benefit Enrollment and Maintenance
  • 270/271 - Eligibility Inquiry and Response
  • 276/277 - Claim Status Request and Response
  • 278 - Healthcare Services Review
  • And all other X12 transaction types (generic processing)

Advanced Features

Return All Intermediate Stages

all_stages = pipeline.transform(
    edi_file="input.edi",
    mapping="mapping.json",
    return_intermediate=True
)

generic_json = all_stages['generic']
structured_json = all_stages['structured']
mapped_json = all_stages['mapped']

Validate Mappings

validation = pipeline.validate_mapping(
    mapping="mapping.json",
    sample_edi="sample.edi"  # Optional
)

if validation['valid']:
    print("Mapping is valid!")
else:
    print("Errors:", validation['errors'])

Custom Lookup Tables

builder = MappingBuilder("mapping_with_lookups")

# Add lookup table
builder.add_lookup_table("status_codes", [
    {"code": "1", "description": "Paid"},
    {"code": "4", "description": "Denied"}
])

# Use in mapping
builder.add_field_mapping(
    "claim_status",
    "$lookupTable('status_codes', 'code', claim_status.code).description"
)

Project Structure

x12_edi_converter/
├── core/
│   ├── parser.py              # X12 to generic JSON parser
│   ├── structured_formatter.py # Generic to structured formatter
│   └── mapper.py              # JSONata-based mapper
├── code_sets/
│   └── edi_codes.py          # EDI code descriptions
├── pipelines/
│   └── transform_pipeline.py # Complete transformation pipeline
├── examples/
│   ├── basic_usage.py        # Basic usage examples
│   └── custom_mapping.py     # Custom mapping examples
└── cli/
    └── main.py               # Command-line interface

Requirements

  • Python 3.8+
  • pyx12 >= 2.3.3
  • jsonata >= 0.2.0

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built on top of the excellent pyx12 library
  • Uses JSONata for powerful JSON transformations
  • Inspired by enterprise EDI processing needs in healthcare

Support

Roadmap

  • Additional transaction type templates
  • Performance optimizations for large files
  • Streaming support for very large EDI files
  • Web-based mapping designer
  • Additional output format support (XML, CSV)
  • EDI validation and compliance checking

Built for the healthcare development community

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyedi-1.0.3.tar.gz (38.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyedi-1.0.3-py3-none-any.whl (36.3 kB view details)

Uploaded Python 3

File details

Details for the file pyedi-1.0.3.tar.gz.

File metadata

  • Download URL: pyedi-1.0.3.tar.gz
  • Upload date:
  • Size: 38.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for pyedi-1.0.3.tar.gz
Algorithm Hash digest
SHA256 21f2c7af780e5d5049359ce19a66d5878bd14a7c2a1cbd87f137acf13686cfab
MD5 a7982e029a3fae1e6f17303f7ea5c5b5
BLAKE2b-256 8e8bca0c67d0d4ee49a8564028c103d27fcb6ff95426102c3556489e2b6efa9b

See more details on using hashes here.

File details

Details for the file pyedi-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyedi-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 36.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for pyedi-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e75c919da25f4015a7b81097426d78c4d5319a000c7ffae0b90c1d6db89b5f8b
MD5 c16a85c31f2d974e2bc9c424359c9a3f
BLAKE2b-256 edbc180d308f8e74bfbcdefdbc6cbe2761e5d6b589e3eaf5f87ca61d200e65a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page