RunTheTests
/

XML to JSON

Convert XML into JSON, with the mapping choices explained.

What you paste

Parsed in your browser — never uploaded

How this reads your output

The document is parsed into a tree, then converted with two conventions applied. Attributes become properties prefixed with an at sign, so they cannot collide with a child element sharing the name. Repeated sibling elements are collected into an array while a single occurrence stays an object, which is the standard approach and also the one that catches people out, since a list with one item converts differently from a list with two.

What the results mean

Attributes prefixed with @
The usual convention, and necessary because XML allows an attribute and a child element with the same name on the same node.
Repeated sibling names
Elements appearing more than once under the same parent become an array. Where this happens, the shape depends on the data rather than on the schema.
Maximum depth
How deeply nested the document is. Deep XML converts to deeply nested JSON, which is often worth flattening before use.

Common problems and fixes

Code broke on a response with only one item
This is the single-element array problem and it is inherent to the conversion rather than a bug here. With one item you get an object; with two you get an array. Any consumer should normalise by wrapping a non-array value in an array before iterating, which costs one line and saves the outage.
Namespaced element names look wrong
Prefixes are kept as part of the key, so an element written as soap:Body becomes a key with the colon in it. That is valid JSON but awkward to access in some languages. Strip prefixes before converting if you do not need them, and keep them if two namespaces genuinely use the same local name.

Frequently asked questions

Why is there no standard XML to JSON mapping?

Because the models differ in ways with no clean resolution. XML has attributes, ordered mixed content, namespaces and comments; JSON has none of those, but does distinguish numbers from strings, which XML does not. Every converter picks conventions, and no two pick identically.

What happens to text mixed with elements?

Text alongside child elements is kept under a #text key. It is a genuinely awkward case, because XML treats a paragraph containing bold text as ordered mixed content and JSON has no way to express that ordering at all.

Are numbers converted?

No. Every value stays a string, because XML has no types without a schema and guessing is how leading zeros and long identifiers get destroyed. Convert deliberately in your own code where you know what a field means.

Put this on your own site

Free to embed, no attribution required beyond the source link the frame carries itself. It runs entirely in your visitor's browser, sets no cookies and loads no third-party script.

Embed code
<iframe src="https://runthetests.com/embed/xml-to-json/" width="100%" height="720" style="border:1px solid #e5e5e5;border-radius:8px" title="XML to JSON" loading="lazy"></iframe>

Preview it at https://runthetests.com/embed/xml-to-json/. Embedded pages are marked noindex, so yours stays the canonical copy — not this one.

More in DevTools