Converting XML to JSON using Python?
--
Track title: CC I Beethoven Sonata No 31 in A Flat M
--
Chapters
00:00 Question
00:47 Accepted answer (Score 70)
01:27 Answer 2 (Score 361)
01:59 Answer 3 (Score 35)
03:03 Answer 4 (Score 17)
03:23 Thank you
--
Full question
https://stackoverflow.com/questions/1915...
Question links:
[weather.com RSS feed]: http://rss.weather.com/weather/rss/local...
Accepted answer links:
[several modules for parsing XML]: http://docs.python.org/2/library/xml.htm...
[json]: http://docs.python.org/2/library/json.ht...
Answer 2 links:
[xmltodict]: https://github.com/martinblech/xmltodict
["standard"]: http://www.xml.com/pub/a/2006/05/31/conv...
[Expat]: http://docs.python.org/library/pyexpat.h...
Answer 3 links:
[xmljson]: https://pypi.python.org/pypi/xmljson
[XML JSON conventions]: http://wiki.open311.org/JSON_and_XML_Con.../
[BadgerFish convention]: http://www.sklar.com/badgerfish/
[GData convention]: http://wiki.open311.org/JSON_and_XML_Con...
[Parker convention]: https://developer.mozilla.org/en-US/docs...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #json #xml #converter
#avk47
ANSWER 1
Score 387
xmltodict (full disclosure: I wrote it) can help you convert your XML to a dict+list+string structure, following this "standard". It is Expat-based, so it's very fast and doesn't need to load the whole XML tree in memory.
Once you have that data structure, you can serialize it to JSON:
import xmltodict, json
o = xmltodict.parse('<e> <a>text</a> <a>text</a> </e>')
json.dumps(o) # '{"e": {"a": ["text", "text"]}}'
ACCEPTED ANSWER
Score 72
There is no "one-to-one" mapping between XML and JSON, so converting one to the other necessarily requires some understanding of what you want to do with the results.
That being said, Python's standard library has several modules for parsing XML (including DOM, SAX, and ElementTree). As of Python 2.6, support for converting Python data structures to and from JSON is included in the json module.
So the infrastructure is there.
ANSWER 3
Score 38
You can use the xmljson library to convert using different XML JSON conventions.
For example, this XML:
<p id="1">text</p>
translates via the BadgerFish convention into this:
{
'p': {
'@id': 1,
'$': 'text'
}
}
and via the GData convention into this (attributes are not supported):
{
'p': {
'$t': 'text'
}
}
... and via the Parker convention into this (attributes are not supported):
{
'p': 'text'
}
It's possible to convert from XML to JSON and from JSON to XML using the same conventions:
>>> import json, xmljson
>>> from lxml.etree import fromstring, tostring
>>> xml = fromstring('<p id="1">text</p>')
>>> json.dumps(xmljson.badgerfish.data(xml))
'{"p": {"@id": 1, "$": "text"}}'
>>> xmljson.parker.etree({'ul': {'li': [1, 2]}})
# Creates [<ul><li>1</li><li>2</li></ul>]
Disclosure: I wrote this library. Hope it helps future searchers.
ANSWER 4
Score 17
To anyone that may still need this. Here's a newer, simple code to do this conversion.
from xml.etree import ElementTree as ET
xml = ET.parse('FILE_NAME.xml')
parsed = parseXmlToJson(xml)
def parseXmlToJson(xml):
response = {}
for child in list(xml):
if len(list(child)) > 0:
response[child.tag] = parseXmlToJson(child)
else:
response[child.tag] = child.text or ''
# one-liner equivalent
# response[child.tag] = parseXmlToJson(child) if len(list(child)) > 0 else child.text or ''
return response