How XML maps to JSON
An element becomes a key under its parent. Attributes become keys with a prefix (@ by default, _ or anything else if you prefer). Text beside attributes or child elements goes under a text key, #text by default. An element with only text becomes a string, and an empty element becomes "".
An element name that repeats under one parent becomes an array. Tick Always use arrays to get arrays even for single elements, which keeps the shape stable when a list happens to have one entry. Values stay strings unless you tick Numbers and booleans, which converts only text such as 42 or true that reads back unchanged.
Namespace prefixes stay in the names (p:book) and xmlns declarations stay as attributes; URIs are not resolved. CDATA becomes plain text. Comments and processing instructions are dropped, the DOCTYPE is skipped and never expanded, and the XML declaration is dropped unless you keep it. The notes under the result say what was left out.
Worked example
Input:
<?xml version="1.0" encoding="UTF-8"?>
<!-- order export -->
<order id="1001" status="paid">
<customer>Ann & Co</customer>
<item sku="A1" qty="2">Widget</item>
<item sku="B2" qty="1">Gadget</item>
<note><![CDATA[Leave at <back> door]]></note>
<gift/>
</order>
Output with the default options:
{
"order": {
"@id": "1001",
"@status": "paid",
"customer": "Ann & Co",
"item": [
{
"@sku": "A1",
"@qty": "2",
"#text": "Widget"
},
{
"@sku": "B2",
"@qty": "1",
"#text": "Gadget"
}
],
"note": "Leave at <back> door",
"gift": ""
}
}
The two item elements became an array, the entity and the CDATA became plain text, the comment and declaration were dropped, and the empty gift became an empty string.
Limits of the conversion
XML has things JSON lacks. If elements of different names are interleaved, grouping them by name loses the interleaving; in mixed content such as Hello <b>x</b> there the text pieces and elements are separated. Malformed XML stops with a line and column. Only the five predefined entities and numeric references are supported, so a DTD-defined entity is reported as an error. To go back, use JSON to XML; for YAML see YAML to JSON.
Frequently asked questions
Why is a single element an object but two are an array?
The converter only creates an array when a name repeats. Use Always use arrays if your code expects a list every time.
Are numbers converted?
Not by default, because XML text has no type. Tick Numbers and booleans to convert text that reads back identically, so 007 and 1.50 stay strings.
What happens to comments and CDATA?
Comments are dropped and counted in the notes. CDATA content is kept as ordinary text, so the CDATA marker itself is not preserved.
Does it resolve namespaces?
No. Prefixes stay in the names and xmlns attributes are kept, but URIs are not looked up or rewritten.
Can I get the same XML back from the JSON?
Often not exactly. Comments, CDATA, attribute order, interleaved elements and the difference between an empty element and an empty string are lost.
Is my XML uploaded?
No. The page parses the XML with its own code in your browser and sends nothing anywhere.