Converting XML to JSON

2016-02-28

Method 1: XSLT

Install xalan to apply the transformation. Get the XSLT script from GitHub. Check and pretty-print the JSON output with jq.

$ sudo apt install xalan jq
$ git clone https://github.com/doekman/xml2json-xslt.git
$ test_xml=$PWD/xml2json-xslt/unittest/nodes.xml
$ xalan -in $test_xml -xsl $PWD/xml2json-xslt/xml2json.xslt | jq .

Method 2: Parse XML into Python objects

Install the Python module xmltodict. Parse the XML into a Python dict and dump to JSON, using this xml2json.py script:

#!/usr/bin/env python3
"""Read in an XML file and dump it straight out to JSON."""
import sys
import json
import xmltodict

try:
    xml_file = sys.argv[1]
except IndexError:
    sys.exit("Convert which XML file?")

with open(xml_file) as f:
    xml = f.read()

data = xmltodict.parse(xml)
print(json.dumps(data))

Check and pretty-print the JSON output with jq.

pip install xmltodict
xml2json.py $test_xml | jq .

The second method seems more robust: the XSLT returns warnings even on its own test set.