XML Tools
XML to JSON
Convert XML to JSON with a stated convention for attributes, text, and repeated elements.
Understand the format
How XML to JSON works
There is no official mapping between XML and JSON, so every converter invents one. The useful question is not whether the output is correct but which convention it chose.
The two formats do not describe the same things
XML has attributes, elements, text, comments, processing instructions, namespaces, and ordering between sibling elements of different names. JSON has objects, arrays, strings, numbers, booleans, and null. There is no lossless way to express the first in the second, which is why no standard mapping exists and why two converters will disagree about the same document.
The most common losses are worth stating plainly. Comments and processing instructions disappear. The distinction between an attribute and a child element becomes a naming convention. Document order between elements of different names is not preserved by an object. And everything arrives as a string, because XML has no types of its own without a schema.
The convention used here
Attributes become keys prefixed with an at sign, so id="1" becomes "@id": "1". Text that sits alongside child elements becomes a "#text" key. An element with no attributes and no children collapses to its text, so <name>Ada</name> becomes "name": "Ada" rather than a nested object. This convention is close to the one popularised by the Badgerfish and xml2json conventions, and it is stated here so you can predict the output rather than discover it.
Repeated sibling elements become an array. That produces the classic trap: <items><item/></items> with one child yields an object, and with two yields an array. Consumers that index the result blindly break the moment a document arrives with a single item. If you control both ends, declare which elements are always collections and normalise them before consuming.
When to convert and when not to
Converting is a good idea when you are reading a document by hand, exploring an unfamiliar feed, or moving data into a system that speaks JSON natively. It is a bad idea as a compatibility layer in a pipeline you control: you inherit the ambiguity above, and every consumer has to know the convention.
Where a schema exists, generating typed objects from the XSD is almost always better than converting to JSON and inferring types at runtime. The conversion here is for reading and prototyping, not as a replacement for a contract.
Step by step
How to use XML to JSON
- Paste the XML document into the input box.
- Read the JSON output, checking how attributes and repeated elements were mapped.
- Look for keys that are objects in one document and arrays in another, and decide how your consumer will handle both.
- Copy the JSON, or open it in the JSON Formatter to reshape it further.
The document is parsed and converted in your browser. Nothing is transmitted, which matters for supplier feeds and configuration files that often carry credentials.
Troubleshooting
Common mistakes and how to fix them
- Indexing a key that is sometimes an object and sometimes an array.
- Normalise on the way in: wrap the value in an array when it is not one, using your knowledge of which elements repeat.
- Expecting "2" to arrive as a number.
- XML text has no type. Convert explicitly, and take the target type from the schema rather than guessing from the value.
- Round-tripping XML to JSON and back and expecting the original document.
- Comments, processing instructions, and the attribute-versus-element distinction do not survive. Keep the original if it matters.
- Converting a signed document.
- An XML signature covers the original bytes. Once converted, it can no longer be verified.