Skip to main content

XML to JSON converter.

#!/usr/bin/env python3

"""
XML to JSON converter.
"""

import json
import sys
from pathlib import Path

from lib import xmltodict # xmltodict module can be found at https://github.com/jabbalaci/Bash-Utils/blob/master/lib/xmltodict.py


def convert(xml_file, xml_attribs=True):
    with open(xml_file, "rb") as f:
        d = xmltodict.parse(f, xml_attribs=xml_attribs)
        return json.dumps(d, indent=4)

#############################################################################

if __name__ == "__main__":
    if len(sys.argv) == 1:
        print('Usage: {0} input.xml'.format(Path(sys.argv[0]).name))
        sys.exit(1)
    # else
    print(convert(sys.argv[1]))