How to convert XML to JSON
- Paste XML, open a file or use From URL. The JSON updates as you type.
- Adjust the options below.
- Press ⇄ to open the result in JSON to XML.
Broken XML is not converted: the output shows the error with its line and column, explained in the XML validator.
Example
<order id="A-1001" paid="true">
<customer zip="02134">Lena Vogel</customer>
<item sku="BK-7" qty="2">Notebook</item>
<item sku="PN-3" qty="1">Fountain pen</item>
<note/>
</order>
With the default options:
{
"order": {
"customer": {
"#text": "Lena Vogel",
"@zip": 2134
},
"item": [
{
"#text": "Notebook",
"@sku": "BK-7",
"@qty": 2
},
{
"#text": "Fountain pen",
"@sku": "PN-3",
"@qty": 1
}
],
"note": "",
"@id": "A-1001",
"@paid": true
}
}
Text next to attributes goes under #text, and the empty <note/> becomes "".
Options
- Attributes: the prefix for attribute keys,
@name,_name,-nameor plainname. Without a prefix, an attribute and a child element of the same name collide. - Numbers & booleans:
"2"becomes2and"true"becomestrue, in text and attributes. It also turnedzip="02134"into2134and reads0x1Fas31, so turn it off for postcodes, phone numbers and IDs. - Always arrays: every element becomes an array, the root included. Without it, two
<item>elements give an array but one gives an object, which breaks code that loops overorder.item.
What JSON cannot keep
- Comments, processing instructions and the
<?xml ?>declaration are dropped; CDATA becomes plain text. - Namespace prefixes stay in the key (
soap:Body), andxmlnsdeclarations appear as attributes. - Mixed content such as
<p>Hello <b>world</b>, again</p>loses its shape: the text pieces join into one#textand their position around<b>is lost.
For a few values, an XPath tester query is quicker than converting. Large results are easier to read in the JSON viewer.