XML to JSON: The Four Things That Do Not Map

XML and JSON both describe trees, which is why the conversion looks easy and why it never quite is. Attributes, repeats, mixed content and namespaces each need a decision.

Why the conversion is not mechanical

JSON has objects, arrays, strings, numbers, booleans and null. XML has elements, attributes, text, and a document model where order always matters. The shapes overlap enough that a converter can produce something, and differ enough that it has to choose how. Two converters given the same XML will produce different JSON, and both will be defensible.

That is why the useful question is not "how do I convert XML to JSON" but "which of the four hard cases does my XML contain, and what should happen to each". The XML to JSON converter makes one set of choices and states them; this article explains what the choices are.

One: attributes have nowhere to go

An XML element can carry attributes alongside its children: `<price currency="USD">19.99</price>`. JSON has no attributes. The converter has to invent a place for them, and the common conventions are a prefixed key such as `@currency` or `_currency`, or a nested object with the attributes under one key and the text under another.

Whichever convention a converter uses, the receiving code has to know it. A price that was `19.99` in XML can come out as `{"@currency": "USD", "#text": "19.99"}`, and code expecting a number gets an object. Check what the converter did with the first attributed element before trusting the rest.

Two: one element or many

Three `<item>` elements in a row obviously become an array. One `<item>` is the ambiguity: is it a single object, or an array that happens to have one entry today? The XML cannot say, and a converter that turns one item into an object and two into an array produces JSON whose shape changes with the data, which breaks every consumer the first time an order has one line.

The fix is usually a rule: elements with a given name are always arrays, declared up front. Where a converter does not offer that, the receiving code has to wrap single objects into arrays itself, and the JSON diff will show exactly where a shape changed between two conversions.

Three: text mixed with elements

`<p>Save <b>20%</b> today</p>` is normal in XML and has no JSON equivalent. The text, the child element and the trailing text are three ordered nodes, and an object cannot hold them in order. Converters either concatenate the text and lose the markup, produce an array of nodes that is faithful and awkward, or keep only the text.

Document-style XML, which is mostly mixed content, converts badly and probably should not be converted. Data-style XML, where every element contains either text or child elements but not both, converts cleanly. Knowing which kind you have is most of the battle.

Four: namespaces

Element names in XML can carry a prefix bound to a namespace URI: `<soap:Envelope>`, `<dc:title>`. JSON keys are plain strings. The converter can keep the prefix in the key, drop it, or replace it with the full URI, and all three produce different keys for the same element.

Dropping prefixes is readable and risks collisions when two namespaces define the same local name. Keeping them is safe and ugly. For a feed or an API response with one namespace, dropping is fine; for a document mixing several, keep them and tolerate the keys.

Numbers, booleans and whitespace

Everything in XML is text. A converter that turns `<count>3</count>` into the number 3 is guessing, and it will also turn a postcode of `01234` into 1234 and a product code of `1e5` into 100000. The safer default is to keep everything as strings and let the consumer convert the fields it knows are numeric. The same applies to `true` and `false`.

Whitespace between elements, which XML preserves, is usually noise in JSON and is stripped. Whitespace inside text, which may be significant, should not be. After conversion, the JSON formatter is the quick way to read the result, and the JSON to CSV converter is the next step when the XML was a table all along.

Questions people ask

Why do different XML to JSON converters give different results?

Because XML has attributes, mixed content, namespaces and an order-matters model that JSON lacks, and every converter has to choose how to represent each. The choices are reasonable and incompatible. Check how your converter handles the first attribute and the first single-element list before relying on the output.

What happens to XML attributes in JSON?

They are placed in invented keys, usually with a prefix such as @ or an underscore, or under a nested attributes object with the element's text under a separate key. An element that was a plain value in XML can become an object in JSON, which downstream code must expect.

Why did one item become an object but two became an array?

Because a single XML element gives the converter no way to know it belongs to a repeating group. Converters that decide by count produce JSON whose shape changes with the data. The fix is a rule naming which elements are always arrays, or wrapping single items in the consuming code.

Should numbers in XML become numbers in JSON?

Only if you know the field is numeric. Everything in XML is text, and automatic conversion turns leading-zero codes into different numbers and exponent-like strings into huge ones. Keeping values as strings and converting known fields afterwards is the safer default.

Can HTML-like XML with inline tags be converted?

Technically, but the result is awkward: text and child elements interleaved in order have no clean JSON form. Converters either flatten to text, losing the markup, or emit node arrays. Document-style content is usually better left as markup.

Can the conversion be reversed?

Only partly. JSON produced by one converter can be turned back into XML by the same converter's rules, but attributes, namespaces and mixed content that were flattened on the way in cannot be reconstructed. Keep the original XML if it may be needed again.

Read next

All articles