XML to JSON: It's Harder Than It Looks
Why converting between XML and JSON produces surprising results, and how to handle the edge cases.
You have XML data. Your JavaScript app needs JSON. Convert it and move on, right?
Except the conversion produces unexpected results. Arrays that should exist don't. Attributes end up in weird places. Single items become arrays randomly.
XML and JSON model data differently. Converting between them requires decisions.
The Fundamental Mismatch
JSON has: objects, arrays, strings, numbers, booleans, null.
XML has: elements, attributes, text content, namespaces, comments, CDATA.
There's no 1:1 mapping.
The Array Problem
XML doesn't have arrays. It has repeated elements.
<users>
<user>Alice</user>
<user>Bob</user>
</users>
This should probably become:
{"users": {"user": ["Alice", "Bob"]}}
But what about:
<users>
<user>Alice</user>
</users>
One element. Is it an array with one item, or just a string? Different converters decide differently.
Consistency matters. If user is sometimes an array and sometimes a string, your code breaks.
The Attribute Problem
XML has attributes. JSON doesn't.
<product id="123">Widget</product>
Common conversion strategies:
{"product": {"_id": "123", "_text": "Widget"}}
{"product": {"@id": "123", "#text": "Widget"}}
{"product": {"$": {"id": "123"}, "_": "Widget"}}
Every convention is different. Know what your converter produces.
The Namespace Problem
XML namespaces create qualified names like soap:Envelope. JSON has no concept of namespaces.
Converters either:
- Include prefixes in property names:
"soap:Envelope" - Strip prefixes:
"Envelope" - Create nested structures for namespace handling
Each approach has trade-offs.
Practical Tips
Be consistent. Pick a conversion library and stick with it. Mixing converters produces inconsistent structures.
Force arrays. If an element can have multiple instances, configure the converter to always produce arrays, even for single elements.
Test edge cases. Convert real data, not just samples. Edge cases in production data will surprise you.
Consider alternatives. If you control both ends, consider using JSON from the start. Converting back and forth loses information.
JSON to XML Challenges
Going the other way has problems too.
No array representation. JSON arrays become repeated elements. You need to name those elements.
{"items": [1, 2, 3]}
Becomes what? <items><item>1</item>...</items>? Where does "item" come from?
Number types. JSON distinguishes numbers from strings. XML doesn't. 123 might become <value>123</value>βis that a number or string?
When to Convert
Integrating with legacy systems. SOAP APIs and older enterprise systems use XML. Convert at the boundary.
Processing XML data. JavaScript handles JSON much better than XML. Convert, process, convert back if needed.
One-time migrations. Moving from XML-based systems to JSON-based ones.
When Not to Convert
Round-trip requirements. XML β JSON β XML often loses information. Attributes, namespaces, ordering.
Schema validation. XML has powerful schema languages (XSD). JSON Schema is less mature.
Document processing. XML's mixed content model (text with embedded elements) doesn't map to JSON cleanly.
XML and JSON represent data differently. Conversion requires choices about arrays, attributes, and namespaces. Understand your converter's behavior and test with realistic data.