pythonjsonxmlxmltodict

Python command-line one-liner using xmltodict to convert XML to JSON


I am trying to write a Python one-liner using xmltodict to convert an XML file to JSON. It seems like there's an issue reading from sys.stdin directly or as sys.stdin.buffer.read():

python -c 'import xmltodict, sys, json; json.dump(xmltodict.parse(sys.stdin, process_namespaces=True), sys.stdout, indent=4);' < foo.xml > bar.json

Solution

  • You need to read() stdin - right now you're just trying to parse the underlying TextIOWrapper:

    python -c 'import xmltodict, sys, json; json.dump(xmltodict.parse(sys.stdin.read(), process_namespaces=True), sys.stdout, indent=4);' < foo.xml > bar.json
                                                                                ^-- Here