How to Convert XML to JSON: Attributes, Arrays and Types Explained
Many older systems, feeds and business files still speak XML, while most web apps, APIs and JavaScript code expect JSON. Converting XML to JSON lets you use that data in modern tools. But the two formats don’t map one-to-one: XML has attributes, mixed text and repeated elements, and JSON has objects, arrays, numbers and true/false.
This guide shows what the free XML to JSON converter does with each part of your XML, and which options to pick so the JSON fits your code.
An example
A small catalog with two books:
<catalog>
<book id="bk101">
<title>XML Basics</title>
<price>29.95</price>
<available>true</available>
</book>
<book id="bk102">
<title lang="en">JSON in Practice</title>
<price>19.50</price>
<available>false</available>
</book>
</catalog>With the default options, it becomes:
{
"catalog": {
"book": [
{
"@id": "bk101",
"title": "XML Basics",
"price": 29.95,
"available": true
},
{
"@id": "bk102",
"title": { "@lang": "en", "#text": "JSON in Practice" },
"price": 19.50,
"available": false
}
]
}
}How each part is converted
- Elements become keys.
<title>becomes"title", and its text becomes the value. - Attributes get a prefix.
id="bk101"becomes"@id", so you can tell attributes from child elements. - Text next to attributes goes in "#text". The second title has an attribute and text, so it becomes an object with
"@lang"and"#text". - Repeated elements become arrays. Two
<book>elements become a list. Every book then has the same shape, which makes the JSON easy to loop over. - Numbers and true/false become real JSON values.
29.95is a number andtruea boolean. Numbers keep their exact digits (19.50stays19.50), and values like007stay text, so ZIP codes and IDs don’t lose their zeros. - Other details: CDATA sections become plain text, empty elements become
null, comments are left out, and names keep their namespace prefix (such asdc:title).
How to convert XML to JSON, step by step
- Open XML to JSON and paste your XML into XML input, click Open file, or drop an .xml file onto the box. Sample loads an example.
- Choose the options described below. The defaults suit most files.
- Click Convert to JSON, then copy or download the result.
If the XML has a mistake, such as an unclosed tag, the converter shows the line and column. See how to format and validate XML for the most common errors.
Choosing the options
Attributes
- @id (default): the most common style, and the one JSON to XML understands, so you can convert back.
- _id: for tools that don’t like the @ sign in names.
- id (no prefix): the cleanest JSON, but an attribute and a child element with the same name would clash.
- Leave out: drops attributes entirely, when you only need the text.
Arrays: the one-item problem
With When an element repeats (default), a catalog with only one book gives an object, not a list:
{ "catalog": { "book": { "@id": "bk101", "title": "XML Basics" } } }Code that expects a list then breaks when a file happens to have a single item. If that matters, choose Every element in an array. Every child element is then a list, even when there’s only one:
{ "catalog": { "book": [ { "@id": "bk101", "title": ["XML Basics"] } ] } }It’s more verbose, but the shape never changes from one file to the next.
Other options
- Numbers and true/false as JSON values: untick it to keep every value as text, exactly as written in the XML.
- Keep the root element: untick it to drop the outer
"catalog"level and start straight from its contents. - Trim spaces around text: removes the line breaks and indentation around values. Untick it only if spaces are part of your data.
- Indentation: 2 spaces, 4 spaces, Tab, or one line to make the JSON as small as possible.
More XML tools
- XML to CSV: open XML records in Excel or Google Sheets.
- XPath Tester: pick out exactly the elements you need before converting.
- XML Formatter and XML Validator: make XML readable and check it.
- Need a spreadsheet from JSON? See how to convert JSON to CSV.
Is my XML uploaded?
No. The conversion runs in your browser, so your data never leaves your device. Our server only records that one conversion happened, for the daily limit.
Frequently asked questions
Can I convert the JSON back to XML?
Yes, with JSON to XML. Keep the @id attribute style so attributes come back as attributes.
Why is my price a number but my ZIP code text?
Values that look like ordinary numbers become numbers. Values with leading zeros, such as 007 or 01234, stay text so nothing is lost.
Is the order of elements kept?
Yes, mostly. Keys appear in the order each element name first appears. Elements with the same name are grouped into one list, in their original order.