Python XML to JSON: Complete Conversion Guide with Examples
- xmltodict is the standard Python library for XML to JSON — install with pip, two lines of code.
- The standard library's ElementTree module also works without any install.
- For quick manual inspection without writing code, a browser converter converts XML to JSON instantly.
Table of Contents
Python has two main ways to convert XML to JSON: the third-party xmltodict library (the most popular, closest to one-liner simplicity) and the built-in ElementTree module (no install required, more verbose). This guide covers both with complete working examples, plus handling for common edge cases like namespaces, attributes, and large files.
Method 1: xmltodict — The Recommended Approach
xmltodict is a third-party library that converts XML to a Python OrderedDict (which maps directly to JSON) with minimal code. Install it:
pip install xmltodict
Convert a string of XML:
import xmltodict, json
xml_string = "<person><name>Alice</name><age>30</age></person>"
data = xmltodict.parse(xml_string)
print(json.dumps(data, indent=2))
Convert from a file:
with open('input.xml', 'r') as f:
data = xmltodict.parse(f.read())
with open('output.json', 'w') as f:
json.dump(data, f, indent=2)
xmltodict handles attributes (mapped to @key convention), repeated elements (converted to lists), CDATA sections, and namespaces out of the box.
Method 2: ElementTree — No Install Required
Python's built-in xml.etree.ElementTree module parses XML without any third-party install. The conversion to JSON is more manual but gives you full control over the output structure.
A recursive function to convert an ElementTree element to a dict:
import xml.etree.ElementTree as ET, json
def elem_to_dict(elem):
d = {elem.tag: {} if elem.attrib else None}
children = list(elem)
if children:
dd = {}
for child in children:
cd = elem_to_dict(child)
if child.tag in dd:
if not isinstance(dd[child.tag], list):
dd[child.tag] = [dd[child.tag]]
dd[child.tag].append(cd[child.tag])
else:
dd.update(cd)
d[elem.tag] = dd
if elem.attrib:
d[elem.tag].update({'@' + k: v for k, v in elem.attrib.items()})
if elem.text and elem.text.strip():
d[elem.tag]['#text' if elem.attrib or children else None] = elem.text.strip()
return d
tree = ET.parse('input.xml')
print(json.dumps(elem_to_dict(tree.getroot()), indent=2))
For most use cases, xmltodict is cleaner — use ElementTree when you need fine-grained control over the output structure.
Sell Custom Apparel — We Handle Printing & Free ShippingHandling XML Namespaces in Python
XML namespaces (like SOAP's soap:Envelope or custom xmlns: declarations) appear in xmltodict output as key names with the full namespace URI, like "{http://schemas.xmlsoap.org/soap/envelope/}Envelope".
To strip namespace prefixes from keys, apply a post-processing step:
import re
def strip_ns(d):
if isinstance(d, dict):
return {re.sub(r'{.*?}', '', k): strip_ns(v) for k, v in d.items()}
elif isinstance(d, list):
return [strip_ns(i) for i in d]
return d
clean = strip_ns(xmltodict.parse(xml_string))
For SOAP responses specifically: xmltodict handles the envelope structure correctly — the namespace prefix is preserved in the key name. If you want clean key names, the strip_ns function above removes namespace URI wrappers.
Large XML Files: Streaming with iterparse
For very large XML files (hundreds of MB or gigabytes), loading the entire file into memory with xmltodict.parse() or ET.parse() may exhaust available RAM. Python's iterparse provides event-driven streaming parsing:
for event, elem in ET.iterparse('large.xml', events=('end',)):
if elem.tag == 'record': # process each record element
print(json.dumps(elem_to_dict(elem)))
elem.clear() # free memory after processing each element
The elem.clear() call is critical — it releases each processed element from memory so the file size doesn't accumulate. With this pattern, you can process XML files of any size with constant memory usage.
For quick manual inspection of a large XML file (not automation), the browser converter handles files up to roughly 20MB in-browser; above that, the Python approach is more reliable.
No Code? Use the Browser Converter
Paste XML, get JSON instantly. No Python, no install, no upload.
Open Free XML to JSON ConverterFrequently Asked Questions
Is xmltodict better than the standard library for XML to JSON?
For most use cases, yes. xmltodict is two lines of code vs. writing a recursive function. It handles attributes, arrays, and namespaces correctly. Use ElementTree when you need custom control.
Why do numbers appear as strings in the JSON output?
XML has no type system — all values are strings. Both xmltodict and ElementTree output them as strings. Add a type-casting step if you need integers or floats in the JSON.
How do I handle invalid XML in Python?
Both xmltodict and ElementTree raise exceptions for malformed XML. Wrap the parse call in a try/except block to catch xml.etree.ElementTree.ParseError (ElementTree) or expat.ExpatError (xmltodict).
Can I convert XML to JSON without installing anything?
Yes. Python's ElementTree + json module are both in the standard library. The function above works with no pip install. For a no-code alternative, the browser converter converts XML to JSON without any setup.

