XML to JSON Converter
Paste XML or drop an .xml file to get JSON. Attributes are kept under a prefix you choose, and malformed XML is reported with the line where it breaks. One of our free JSON tools.
XML
JSON
How to convert XML to JSON
- Step 1
Paste the XML
Paste it or drop an .xml, .rss or .svg file.
- Step 2
Choose how attributes look
Set the attribute prefix, drop attributes, and choose whether numbers and booleans are parsed.
- Step 3
Copy the JSON
Copy it, download data.json, or send it to JSON to TypeScript.
Worked example
#text. The price becomes a number, but the ISBN keeps its leading zero as a string.
<catalog>
<book id="bk101">
<title>Dune</title>
<price currency="USD">9.99</price>
<isbn>0441013597</isbn>
</book>
</catalog> {
"catalog": {
"book": {
"title": "Dune",
"price": {
"#text": 9.99,
"@_currency": "USD"
},
"isbn": "0441013597",
"@_id": "bk101"
}
}
} How XML maps to JSON
Each element becomes a key named after its tag. An element that contains only text becomes a plain value; one with child elements becomes an object. When the same element appears more than once under one parent, its values are collected into an array in document order. An element that appears once stays a single value, not an array of one. That is the most common surprise in XML-to-JSON code: a feed with one <item> gives an object, while the same feed with two gives an array. Handle both shapes when you read the result.
Attributes
Attributes go into the element's object with a prefix in front of their names, @_ by default, so they can never clash with a child element of the same name. Set the prefix to @ to match other tools, or clear it for plain names if you know there are no clashes. Tick Drop attributes to ignore them entirely, which gives simpler JSON when attributes only hold formatting or IDs you do not need.
When an element has attributes and text, as in <price currency="USD">9.99</price>, the text goes under a #text key next to the attributes.
Numbers, booleans and text
XML has no types, so everything is text until you decide otherwise. With Parse numbers and booleans on, text and attribute values that look like decimal numbers become numbers and true and false become booleans. Values with leading zeros, such as ISBNs, ZIP codes and phone numbers, stay strings so no digits are lost, and hexadecimal-looking values are not converted. Turn the option off to keep every value as a string. Whitespace around text is trimmed, and CDATA sections are merged into the element's text.
Well-formedness check
Before converting, the XML is checked for structural errors: tags that do not match or are never closed, unquoted or repeated attributes, and text outside the root element. The error names the problem and gives its line and column, which are highlighted in the input. The check is for well-formedness only. The document is not validated against a DTD or XSD schema, and the XML declaration, processing instructions and comments are left out of the JSON.
Typical inputs
RSS and Atom feeds, SOAP responses, sitemap files, Android resource files, SVG images and configuration exports all convert the same way. Namespace prefixes are kept as part of the name, so a SOAP body appears under soap:Envelope. To shape the result further, send it to the JSON formatter or JSON to TypeScript.
Questions and answers
Why is one element an object and another an array?
An element that appears once becomes a single value, and one that repeats becomes an array. So a feed with one <item> gives an object where a feed with two gives an array. Check for both shapes in code that reads the JSON.
Where do attributes go?
Into the element's object, with the attribute prefix in front of the name: <price currency="USD"> gives "@_currency": "USD". Set the prefix to @ or leave it empty, or tick Drop attributes to ignore them.
What is the #text key?
When an element has both attributes and text, the text goes under #text. <price currency="USD">44.95</price> becomes {"#text": 44.95, "@_currency": "USD"}.
Does it validate against a DTD or XSD?
No. It checks that the XML is well-formed: tags match and close, attributes are quoted and not repeated. It does not check the document against a schema.
Is the XML uploaded?
No. It is parsed with fast-xml-parser inside your browser, in a Web Worker for files over 1 MB.
Related tools
All JSON & Schema toolsNext step
Need this built into your stack?
The team behind SyntaxSnap builds custom code generators, internal developer tools and API integrations.