Converting XML to JSON using Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting XML to JSON in Python is easy at the syntax level and tricky at the data-model level. XML and JSON do not map perfectly, so the real job is not just parsing the file, but deciding how to represent attributes, repeated elements, text nodes, and namespaces in a JSON shape that your application can actually use.
The Quick Path with xmltodict
For straightforward conversions, xmltodict is the most convenient tool. It parses XML into nested Python dictionaries and lists, which you can then serialize with the standard json module.
This is the fastest way to get from XML text to JSON text, and it is usually good enough when the XML structure is regular and the consumer can tolerate library conventions.
How Attributes and Text Are Represented
XML has features that JSON does not have natively. xmltodict handles that by using conventions:
- attributes are typically stored under keys prefixed with
@ - text content may be stored under a key such as
#text
That means the converted structure is not a universal standard. It is one library's mapping choice. If another system expects a different JSON shape, you may need a custom conversion step.
Repeated Elements Become Lists
One of the most important edge cases is repeated XML tags. In JSON, repeated children usually become a list:
That prints a Python list. However, if the source XML sometimes has one item and sometimes many, the resulting shape can vary between a scalar value and a list. That inconsistency is a common source of bugs.
Custom Conversion with ElementTree
If you need full control over the output shape, parse the XML yourself and build the JSON structure explicitly. The standard library's xml.etree.ElementTree is enough for many cases.
This takes more work, but it gives you predictable control over how repeated tags, missing values, or attributes should appear.
File-Based Conversion
Converting a file is just a small extension of the same pattern:
The conversion itself is simple. The harder part is deciding whether the default mapping matches the schema expectations of the next system.
Namespaces and Mixed Content
XML namespaces can make tag names verbose, and mixed content can make the tree harder to flatten cleanly into JSON. If the source documents use namespaces heavily or mix text with nested elements inside the same node, expect to write custom handling instead of relying on a one-line conversion.
That is not a Python limitation. It is a consequence of XML being richer than plain JSON objects.
Common Pitfalls
- Assuming XML and JSON have a one-to-one structural mapping.
- Forgetting that repeated elements may need consistent list handling.
- Ignoring attributes and then losing important metadata during conversion.
- Using a library default shape when the downstream API expects a different JSON contract.
- Underestimating namespaces and mixed content in real-world XML feeds.
Summary
- '
xmltodictis the easiest way to convert simple XML to JSON in Python.' - XML attributes, repeated tags, and text nodes need explicit mapping choices.
- Use
ElementTreewhen you need a custom JSON shape. - File conversion is simple once the mapping rule is clear.
- The hard part is not parsing XML, but choosing a JSON representation that stays consistent.

