'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 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 b_c