How to use XML to JSON
- Paste your XML.
- The JSON equivalent appears alongside it.
- Check how repeated elements and attributes were mapped before writing code against the shape.
- Attributes and text content need distinct conventions in JSON; check the output shape rather than assuming.
- Comments, processing instructions and namespace declarations do not survive the conversion.
- An element that appears once produces an object; the same element repeated produces an array.
XML carries things JSON has no slot for
An XML element can have attributes as well as child elements; JSON objects have only keys. XML preserves the order of mixed content and distinguishes an empty element from an absent one; JSON does neither. XML has namespaces, comments, processing instructions and CDATA sections. Every XML-to-JSON converter therefore invents conventions — typically prefixing attributes with @ and putting text content under a key such as #text — and no two converters agree on them. Round-tripping XML through JSON and back is not reliable.
The single-element array problem
This is the one that breaks production code. Repeated child elements become a JSON array, so three <item> elements give you a list. But a document with exactly one <item> gives an object, not an array of one — there is nothing in the XML to say the element repeats. Code written against a multi-item sample then crashes on a single-item response. Either normalise with a schema that declares which elements are collections, or write consuming code that accepts both shapes.
Types are your problem, not XML’s
Everything in XML is text. <age>28</age> is the string “28” unless a schema says otherwise, so a converter either leaves everything as strings — safe, and annoying — or guesses, which reintroduces the problem of version numbers and leading zeros becoming numbers. When the output feeds code you control, prefer strings and convert explicitly at the point of use.
Frequently asked questions
How are XML elements converted?
What happens to XML attributes?
Can it handle complex XML?
Can I convert the JSON back to the original XML?
Why did my list become an object?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.