Skip to content

Instantly share code, notes, and snippets.

@gtors
Created August 6, 2021 11:26
Show Gist options
  • Save gtors/064fba52a812715151cca91aa485e7f7 to your computer and use it in GitHub Desktop.
Save gtors/064fba52a812715151cca91aa485e7f7 to your computer and use it in GitHub Desktop.
Script for converting xml to json
#!/usr/bin/env python
"""xml2json.py Convert XML to JSON
Relies on ElementTree for the XML parsing. This is based on
pesterfish.py but uses a different XML->JSON mapping.
The XML->JSON mapping is described at
http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html
Rewritten to a command line utility by Hay Kranen < github.com/hay >
XML JSON
<e/> "e": null
<e>text</e> "e": "text"
<e name="value" /> "e": { "@name": "value" }
<e name="value">text</e> "e": { "@name": "value", "#text": "text" }
<e> <a>text</a ><b>text</b> </e> "e": { "a": "text", "b": "text" }
<e> <a>text</a> <a>text</a> </e> "e": { "a": ["text", "text"] }
<e> text <a>text</a> </e> "e": { "#text": "text", "a": "text" }
This is very similar to the mapping used for Yahoo Web Services
(http://developer.yahoo.com/common/json.html#xml).
This is a mess in that it is so unpredictable -- it requires lots of testing
(e.g. to see if values are lists or strings or dictionaries). For use
in Python this could be vastly cleaner. Think about whether the internal
form can be more self-consistent while maintaining good external characteristics
for the JSON.
Look at the Yahoo version closely to see how it works. Maybe can adopt
that completely if it makes more sense...
R. White, 2006 November 6
Usage:
from xml2json
import json
j = json.loads(xml2json(raw_xml))
"""
import json as jsonlib
import optparse
import os
import sys
import xml.etree.cElementTree as ET
def elem_to_internal(elem, strip=1):
"""Convert an Element into an internal dictionary (not JSON!)."""
d = {}
for key, value in list(elem.attrib.items()):
d["@" + key] = value
# loop over subelements to merge them
for subelem in elem:
v = elem_to_internal(subelem, strip=strip)
tag = subelem.tag
value = v[tag]
try:
# add to existing list for this tag
d[tag].append(value)
except AttributeError:
# turn existing entry into a list
d[tag] = [d[tag], value]
except KeyError:
# add a new non-list entry
d[tag] = value
text = elem.text
if strip:
# ignore leading and trailing whitespace
if text:
text = text.strip()
if d:
# use #text element if other attributes exist
if text:
d["#text"] = text
else:
# text is the value if no attributes
d = text or None
return {elem.tag: d}
def elem2json(elem, strip=1):
"""Convert an ElementTree or Element into a JSON string."""
if hasattr(elem, "getroot"):
elem = elem.getroot()
return jsonlib.dumps(elem_to_internal(elem, strip=strip))
def xml2json(xmlstring, strip=1):
"""Convert an XML string into a JSON string."""
elem = ET.fromstring(xmlstring)
return elem2json(elem, strip=strip)
def main():
p = optparse.OptionParser(
description="Converts XML to JSON or the other way around",
prog="xml2json",
usage="%prog file.xml > file.json",
)
p.add_option("--out", "-o", help="Write to OUT instead of stdout")
options, arguments = p.parse_args()
if arguments:
# check if this file exists
if os.path.isfile(arguments[0]):
input_name = arguments[0]
else:
sys.exit(-1)
else:
p.print_help()
sys.exit(-1)
input = open(input_name).read()
out = xml2json(input, strip=0)
if options.out:
file = open(options.out, "w")
file.write(out)
file.close()
else:
print(out)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment