Format Converter for EPCIS 2.0 and EPCIS 1.2
OpenEPCIS solution to convert EPCIS 2.0 and 1.2 document or single event from XML to JSON/JSON-LD and vice versa.
Quick links
The format converter is available as a web application, as an API and as source code:
- Format converter web application: https://tools.openepcis.io/ui/format-converter
- Format converter API endpoint: https://tools.openepcis.io/q/swagger-ui/#/Format%20Converter
- Format converter application code: https://github.com/openepcis/openepcis-document-converter
Introduction
Up to version 1.2, EPCIS was an XML-only standard. EPCIS 2.0 added JSON/JSON-LD as a first-class format, so organizations now have to move documents between the two without losing detail: XML arrives from one partner, JSON-LD goes out to another, or the other way round.
The OpenEPCIS format converter handles that translation for the full EPCIS document: events, user extensions, sensor elements, master data blocks and the semantics of the GS1-Extensions header. The open-source converter is based on XSLT, which works well for single events and small batches with plain event structures. For exports of several gigabytes, deep extension trees and mixed 1.2 and 2.0 corpora, the Business edition adds a SAX-streaming converter that processes documents with bounded memory and plugs directly into the validation and event-hash pipeline.
Both converters produce documents that conform to the EPCIS 2.0 schema.
Usage
You can convert an EPCIS document in the browser, through the API or from the command line.
Web application
The web application takes an EPCIS document in XML or JSON/JSON-LD and returns it in the other format. You can open it here.
Swagger-UI
The format converter API accepts an EPCIS document and returns the converted document in its response. You can call it from your own application's code or try it online through Swagger-UI, which is available here.
Command Line
cURL (Client URL) is a widely used command-line tool for sending data to a server and receiving its response, and many developers prefer it to a web interface. To convert a document with cURL, post it to
https://tools.openepcis.io/api/convert/json/2.0 or https://tools.openepcis.io/api/convert/xml/2.0, depending on the target format. The following request converts a JSON document to XML:
curl -X 'POST' \
'https://tools.openepcis.io/api/convert/xml/2.0' \
-H 'accept: application/xml' \
-H 'Content-Type: application/json' \
-d '{
"@context": ["https://gs1.github.io/EPCIS/epcis-context.jsonld",{"example": "http://ns.example.com/epcis/"}],
"id": "https://id.example.org/document1",
"type": "EPCISDocument",
"schemaVersion": "2.0",
"creationDate":"2019-11-01T14:00:00.000+01:00",
"epcisBody": {
"eventList": [
{
"eventID": "ni:///sha-256;025ac144187a8c5e14caf4d1cfa69250a33dc59a5bc42a68d31b1b5e55a3f15a?ver=CBV2.0",
"type": "AssociationEvent",
"eventTime": "2019-11-01T14:00:00.000+01:00",
"eventTimeZoneOffset": "+01:00",
"parentID":"urn:epc:id:grai:4012345.55555.987",
"childEPCs":["urn:epc:id:giai:4000001.12345"],
"action": "ADD",
"bizStep": "assembling",
"readPoint": {"id": "urn:epc:id:sgln:4012345.00001.0"}
}
]
}
}'
Application Code
The converter is written mainly in Java. Its code is published on the OpenEPCIS GitHub account, where you can read it directly or use it as a dependency in your own projects. The complete code is available here.
Converting XML to JSON/JSON-LD document
To convert an EPCIS 2.0 XML document to JSON/JSON-LD, pass the XML data as an InputStream to the conversion
method of VersionTransformer.class:
final InputStream xmlStream=getClass().getResourceAsStream("/convert/xmlDocument.xml");
final Conversion conversion=Conversion.builder()
.generateGS1CompliantDocument(false)
.fromMediaType(EPCISFormat.XML)
.fromVersion(EPCISVersion.VERSION_2_0_0)
.toMediaType(EPCISFormat.JSON_LD)
.toVersion(EPCISVersion.VERSION_2_0_0)
.build();
final InputStream convertedDocument=new VersionTransformer().convert(xmlStream,conversion);
//System.out.println("Converted Version Transformer JSON : \n"+IOUtils.toString(convertedDocument,StandardCharsets.UTF_8));
Converting JSON/JSON-LD to XML document
To convert an EPCIS 2.0 JSON/JSON-LD document to XML, pass the JSON/JSON-LD data as an InputStream to the conversion
method of VersionTransformer.class:
final InputStream jsonStream=getClass().getResourceAsStream("/convert/JsonDocument.json");
final Conversion conversion=Conversion.builder()
.generateGS1CompliantDocument(false)
.fromMediaType(EPCISFormat.JSON_LD)
.fromVersion(EPCISVersion.VERSION_2_0_0)
.toMediaType(EPCISFormat.XML)
.toVersion(EPCISVersion.VERSION_2_0_0)
.build();
final InputStream convertedDocument=new VersionTransformer().convert(jsonStream,conversion);
//System.out.println("Converted XML document : "+IOUtils.toString(convertedDocument,StandardCharsets.UTF_8));
Dependencies
The conversion logic depends on the openepcis-models package.