Skip to content
shellhacks.dev

XML to JSON Converter

Convert XML to JSON, with attributes prefixed, text in #text and repeated elements as arrays, optionally always arrays for a stable shape.

Free · No sign-up · Your inputs stay in your browser

Input

Convert XML to JSON

Output

Not well-formed XML

DOMParser is not defined

XML has no single mapping to JSON. Here attributes get a prefix, mixed text goes in #text, and a repeated element becomes an array. That last rule means one entry and two entries give different shapes; 'always use arrays' makes the shape stable for code that consumes it.

Runs in this browser. Nothing you paste is sent to a server.

There is no single mapping

XML has attributes, mixed content, namespaces and ordered children; JSON has none of them. Every converter picks a convention, and this one follows the common '@ for attributes, #text for text' style used by xml2js and similar libraries. Order between different child elements is lost.

The one-or-many problem

An element that appears once becomes an object, and twice an array. Code written against a response with one item then breaks on the day there are two. 'Always use arrays' removes that trap at the cost of more brackets. PHP's simplexml_load_string() has the same ambiguity, which is why json_encode(simplexml_load_string($xml)) is a fragile shortcut.

Share

Frequently asked questions

How are XML attributes represented?
As keys with a prefix, @ by default, alongside the child elements: <item id="1"> becomes {"@id": "1"}.
Are numbers converted?
No. XML text has no type, so every value stays a string. Convert the fields you need after reading them.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Go deeper

Suggested reading

Keep planning

Similar tools