Convert XML to JSON

Turn an XML document into JSON.

JSON

About this converter

Most often to get an old feed, an export or a SOAP response into something a modern codebase can read without ceremony.

The awkward part is that XML distinguishes attributes from child elements and JSON has only keys, so the two would collide — an id attribute and an <id> child cannot both be "id". Attributes are therefore prefixed with @_, and element text that sits alongside attributes is kept under #text. Nothing is thrown away, which is what converters that drop attributes do.

Repeated elements become a list. An element that appears once becomes a single value rather than a list of one — which is XML's oldest ambiguity, since nothing in a document says whether a single <item> is one item or a list that happens to have one entry in it.

Frequently asked questions

What are the @_ keys?

Attributes. XML keeps attributes and child elements in separate namespaces and JSON does not, so an attribute called id and a child element called id would overwrite each other. The prefix keeps both and records which was which.

What is #text?

The text content of an element that also has attributes. Ada cannot become just "Ada" without losing the role, so it becomes an object with @_role and #text inside it.

Why is a single element not a list?

Because nothing in the XML says it should be. A document with one is indistinguishable from a list containing one item — only a schema knows, and a schema is usually not to hand. If your code expects a list, check for the single-object case.

Does what I paste get sent anywhere?

No. The conversion runs in your browser, on your own machine, which is why the result appears as you type. What you paste is not uploaded or logged.

Related tools