Converting XML to JSON


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
$ 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 script:

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

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

with open(xml_file) as f:
    xml =

data = xmltodict.parse(xml)

Check and pretty-print the JSON output with jq.

pip install xmltodict $test_xml | jq .

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