Convert XML to JSON with Python
If you want to convert an XML document into a JSON document in Python, you can use the functions of the xmltodict module by first feeding your XML file into a Python dictionary and then converting this dictionary with the Python json module (import json) converts and saves. This can then look like this:
import xmltodict , json
with open ( 'your-xml-file.xml' , 'r' , encoding = 'utf-8' , errors = 'ignore' ) as myfile:
obj = xmltodict.parse(myfile.read())
myfile.close()
json_data = json.dumps(obj , indent = 4 )
f = open ( "your-created-json-file.json" , "w" )
f.write(json_data)
f.close()
Leave a comment
Please note, comments need to be approved before they are published.