How to Convert XML to JSON
To convert XML to JSON, paste the document into a converter and run it. Elements become object keys, attributes are preserved with a prefix to distinguish them from child elements, and repeated sibling elements become arrays. The conversion is not perfectly lossless, because XML has concepts — attributes, namespaces, mixed content, comments — that JSON has no direct equivalent for.
XML to JSON Converter
Convert any XML document into readable JSON.
XML and JSON model data differently, so converting between them always involves a convention. XML distinguishes an attribute from a child element; JSON has only keys. XML allows text and elements to be mixed inside one element; JSON has no way to express that. Any converter must decide how to encode those differences.
A word on safety: XML parsers that resolve external entities can be induced to read local files or make outbound network requests. That is the XXE class of vulnerability, and it is why parsing here is done with entity resolution disabled.
Step by step
-
Paste the XML
Include the root element. A fragment with several top-level elements is not a well-formed XML document and will be rejected before conversion starts.
-
Fix well-formedness errors first
Unclosed tags, mismatched nesting and unescaped ampersands all stop the parse. The error position tells you where the document became impossible to parse.
-
Check how attributes came through
Attributes are preserved with a distinguishing prefix rather than merged with child elements, so an id attribute and an id child element do not collide.
-
Watch the single-versus-repeated element trap
Repeated sibling elements become a JSON array. When a list happens to contain exactly one item, it can convert to a single object instead of a one-element array — handle both shapes in code that consumes the output.
-
Format the result
Run the JSON through a formatter to check the nesting is what you expected, particularly around arrays and attributes.
Example
The id attribute is preserved with a prefix; two <item> siblings become an array.
XML
<catalogue>
<item id="1">Alpha</item>
<item id="2">Beta</item>
</catalogue>
JSON
{
"catalogue": {
"item": [
{ "@id": "1", "#text": "Alpha" },
{ "@id": "2", "#text": "Beta" }
]
}
}
Frequently asked questions
Is the conversion lossless?
How are attributes represented?
Why did my list become an object?
Are external entities processed?
Why does my document fail with an undefined entity error?
Tools used in this guide
All tools →- XML to JSON Converter Convert any XML document into readable JSON. In your browser
- JSON to XML Converter Convert JSON objects and arrays into well-formed XML. In your browser
- XML Formatter Indent XML so nested documents become readable. In your browser
- XML Validator Check that an XML document is well-formed. In your browser
- JSON Formatter Beautify, validate and sort JSON with precise error messages. In your browser
Related guides
All guides →- JSON vs XML Why JSON replaced XML for most APIs, and the cases where XML is still the better answer.
- How to Format JSON Turn minified or broken JSON into something readable, and fix it when it will not parse.
- How to Convert CSV to JSON Turn a spreadsheet export into a clean JSON array, with the delimiter and header options that matter.
Last reviewed .