EDI to JSON

Exporting or importing EDI to or from JSON

The quickest method of converting between JSON text and a .NET object is using the JsonSerializer. All EDI templates are marked for JSON serialization.

1. Serialize EDI to JSON using Newtonsoft's Json.Net

//  Read EDI file
List ediItems;
using (var ediReader = new X12Reader(ediStream, factory))
    ediItems = ediReader.ReadToEnd().ToList();

//  Pull all EDI objects
var transactions = ediItems.OfType<TS850>();

//  Serialize each EDI object to JSON
foreach (var transaction in transactions)
    var json = Newtonsoft.Json.JsonConvert.SerializeObject(transaction);

 

 

 


 

 

 

2. Deserialize to EDI from JSON using Netonsoft's Json.Net

//  Read JSON string
var ediJson = File.OpenRead(@"C:\PurchaseOrder.json").LoadToString();

//  Deserialize to EDI object
var transaction = Newtonsoft.Json.JsonConvert.DeserializeObject<TS850>(ediJson);

 

 

 


 

 

 

Sample JSON String

{
	"ST": {
		"TransactionSetIdentifierCode_01": "850",
		"TransactionSetControlNumber_02": "0001"
	},
	"BEG": {
		"TransactionSetPurposeCode_01": "00",
		"PurchaseOrderTypeCode_02": "SA",
		"PurchaseOrderNumber_03": "XX-1234",
		"Date_05": "20170301",
		"AcknowledgmentType_07": "NA"
	},
	"PER": [{
		"ContactFunctionCode_01": "BD",
		"Name_02": "ED SMITH",
		"CommunicationNumberQualifier_03": "TE",
		"CommunicationNumber_04": "8001234567"
	}],
	"TAX": [{
		"TaxIdentificationNumber_01": "53247765",
		"LocationQualifier_02": "SP",
		"LocationIdentifier_03": "CA",
		"TaxExemptCode_12": "9"
	}],
	"N1Loop": [{
		"N1": {
			"EntityIdentifierCode_01": "BY",
			"Name_02": "ABC AEROSPACE",
			"IdentificationCodeQualifier_03": "9",
			"IdentificationCode_04": "1234567890101"
		},
		"N2": [{
			"Name_01": "AIRCRAFT DIVISION"
		}],
		"N3": [{
			"AddressInformation_01": "2000 JET BLVD"
		}],
		"N4": [{
			"CityName_01": "FIGHTER TOWN",
			"StateorProvinceCode_02": "CA",
			"PostalCode_03": "98898"
		}]
	}]
	"PO1Loop": [{
		"PO1": {
			"AssignedIdentification_01": "1",
			"QuantityOrdered_02": "25",
			"UnitorBasisforMeasurementCode_03": "EA",
			"UnitPrice_04": "36",
			"BasisofUnitPriceCode_05": "PE",
			"ProductServiceIDQualifier_06": "MG",
			"ProductServiceID_07": "XYZ-1234"
		},
		"MEA": [{
			"MeasurementReferenceIDCode_01": "WT",
			"MeasurementQualifier_02": "WT",
			"MeasurementValue_03": "10",
			"CompositeUnitofMeasure_04": {
				"UnitorBasisforMeasurementCode_01": "OZ"
			}
		}],
		"IT8": {
			"ProductServiceSubstitutionCode_07": "B0"
		},
		"SCHLoop": [{
			"SCH": {
				"Quantity_01": "25",
				"UnitorBasisforMeasurementCode_02": "EA",
				"DateTimeQualifier_05": "106",
				"Date_06": "20170615"
			}
		}]
	}],
	"CTTLoop": {
		"CTT": {
			"NumberofLineItems_01": "1"
		},
		"AMT": {
			"AmountQualifierCode_01": "TT",
			"MonetaryAmount_02": "900"
		}
	},
	"SE": {
		"NumberofIncludedSegments_01": "15",
		"TransactionSetControlNumber_02": "0001"
	},
	"Name": "850",
	"Version": "004010",
	"Format": "X12",
	"PartsCount": 0,
	"IsPart": false,
	"SplitPostion": 0,
	"HasErrors": false,
	"ErrorContext": {
		"Name": "850",
		"ControlNumber": "0001",
		"Version": "004010",
		"Codes": [],
		"Errors": [],
		"HasErrors": false
	}
}
Was this article helpful?
0 out of 1 found this helpful