Here are 2 python scripts which convert XML to JSON and JSON to XML.
XML to JSON
Create the sample XML file, with the below contents.
sample.xml
<planets> <planet> <name> Earth </name> <radius> 6,371km </radius> </planet> <planet> <name> Jupiter </name> <radius> 69,911km </radius> </planet> <planet> <name> Mars </name> <radius> 3,390km </radius> </planet> </planets>
Run the below python script and and it will output the converted XML as a file named output.json.
import json import xmltodict with open("sample.xml", 'r') as f: xmlString = f.read() print("XML input (sample.xml):") print(xmlString) jsonString = json.dumps(xmltodict.parse(xmlString), indent=4) print("\nJSON output(output.json):") print(jsonString) with open("output.json", 'w') as f: f.write(jsonString)
JSON to XML
Create the sample JSON file, with the below contents.
sample.json
{ "planets": { "planet": [ { "name": "Earth", "radius": "6,371km" }, { "name": "Jupiter", "radius": "69,911km" }, { "name": "Mars", "radius": "3,390km" } ] } }
Run the below python script and and it will output the converted JSON as a file named output.xml.
import json import xmltodict with open('sample.json', 'r') as f: jsonString = f.read() print('JSON input (sample.json):') print(jsonString) xmlString = xmltodict.unparse(json.loads(jsonString), pretty=True) print('\nXML output(output.xml):') print(xmlString) with open('output.xml', 'w') as f: f.write(xmlString)
You may need to install the xmltodict module:
pip install xmltodict
This does not work accurately. If you convert a XML to Json and then convert the same JSON to XML_2. XML and XML_2 are not same.
Cheers for the heads up I’ll double check. As there’s no processing being done on it then it must be the case that it’s a bug in python or those two libraries.
Edit: Just tried it and it worked fine going from xml->json->xml, can you please give an example input xml?
Can i get any hint to convert json from url(which is large json file) to xml file using python?
Hey there, any thoughts on how to do this with an XML file hosted online rather than a local file?
Sure, I would probably download the file using the python requests library and then pass it to the functions described here. Hth
sir, how to correct fllowing error:
Traceback (most recent call last):
File “C:/Python27/output2.py”, line 2, in
with open(‘sample.json’) as f:
FileNotFoundError: [Errno 2] No such file or directory: ‘sample.json’
This means that the python script cannot find the sample.json file. Make sure it is in the same directory for this particular example.
Thank you very much for this, It’s been more than helpful to me but ma finding a hard time converting a big file of about 12 MBS…
I don’t know how you can go about it
Thank you very much though
ValueError: document with multiple roots
Hi, is there a way to preserve order of subelements? I mean from xml:
text1
text2
we get such json:
{“title”: {
“p”: [
“text1”,
“text2”
],
“newline”: null
}}
When we’re trying to convert the json back to xml we get different structure.
Or do I just need different tool? 😉
Sorry, xml tags are lost. may be this can work
text1
text1
Well, try this (hope you can guess where tags should start)
title>
p> text1 /p>
newline/>
p> text1 /p>
/title>