XML-to-JSON

XML to JSON and Back Again (the Easy Way)

XML to JSON and Back Again (the Easy Way)

Posted on: August 3, 2021


We have published a project in LIXILab that demonstrates a number of methods for converting LIXI2 messages between XML and JSON showing how fast and easy these conversions are. We also explain some of the pitfalls that you need to watch out for when using a generic XML to JSON converter.

We use this project as part of our automated CI/CD testing framework before every new release by converting sample LIXI2 messages from XML to JSON using three different techniques and back to XML again.

Each technique results in identical messages after conversion, validated by executing the transformations on every sample in our samples repository (also available to LIXI members here) and confirming that resulting files are identical.

The three techniques demonstrated are:

  1. Using the LIXI Python Package
  2. XSLT executed with Python
  3. XSLT executed with JavaScript

The LIXI Python Package only requires a few lines of code and is perfect for executing conversion on the desktop (see the xml_to_json.py script). Full instructions on setting up and using the LIXI PyPI Package are available here.

Using XSLT to transform a LIXI2 message from XML to JSON is the most performant approach. We demonstrate XSLT with Python in the xml_to_json_xslt.py script.

To demonstrate the portability of XSLT transformations, we have also shown how to use the same XSLT instructions, but this time executed with JavaScript in the xml_to_json_xslt.js script. The same XSLT can be executed in any programming language.

Why can't I use a generic XML to JSON converter?

Using a generic XML to JSON converter on a LIXI2 XML message does not always produce a JSON message that will validate against the LIXI2 JSON Schema. There are subtle differences in the specifications of JSON and XML that requires these conversions to be schema-aware.

XML messages carry no information about the repeatability of an element - in observing a single non-repeated element in an XML message, one can't know if it might be repeatable without referring to the schema. A design decision was made by the community that repeatable elements would be represented in the JSON as an array of objects, even if only a single instance occurs. Therefore any converter needs to refer to the schema to know whether to place an element in an array or as a singular object.

In addition, XML messages carry no information about attribute data types. For example, a string of value "1" and a numeric value of "1" are both represented within quotation marks. Since JSON represents integers without quotation marks and strings within quotations any converter must refer back to the schema to correctly determine its data type to represent it correctly in the resulting JSON.

For these two reasons, a schema-aware converter is required to correctly convert XML to JSON rather than a generic converter.

Note that using this XSLT is a one-way conversion (from XML to JSON), but many generic converters will work or alternatively use the Python Package used above.

Access the Demo XML-to-JSON Project Repository

The demonstration of the conversion of LIXI messages from XML to JSON is available in a project in LIXILab, our source code repository - members can register for a LIXILab account here.


Related Blogs


Written by:
John Matthews, LIXI Technical Lead
First Published: August 3, 2021