Hunt
Skip to content

XML to JSON

Paste XML to get JSON you can work with in JavaScript, APIs and config files. Attributes, text and repeated elements are mapped the same way every time, values stay as written unless you choose to parse them, and invalid XML is reported with its line and column.

Runs in your browser

How XML maps to JSON

XML has things JSON doesn't (attributes, mixed text and elements, repeated elements), so every converter has to choose a convention. This one uses the most common:

  • Each element becomes a property named after it. An element with only text becomes a string: <customer>Ada</customer> gives "customer": "Ada".
  • Attributes become properties with a prefix, @ by default: <order id="1042"> gives "@id": "1042".
  • When an element has attributes or children as well as text, the text goes under #text.
  • Repeated elements become an array. An element that appears once stays a single value, unless Always arrays is on.
  • Empty elements such as <note/> become empty strings. Comments and processing instructions are dropped.

How to use the XML to JSON

  1. Paste XML, or press Sample.
  2. Choose the attribute prefix and text key your code expects.
  3. Turn on Always arrays or Parse values if needed.
  4. Copy or download the JSON.

Examples

Attributes, text and a repeated element

Input
<order id="1042" status="paid">
<customer>Ada Lovelace</customer>
<item sku="A-1" qty="2">Notebook</item>
<item sku="B-7" qty="1">Pen &amp; ink</item>
<note/>
</order>
Output
{
"order": {
"@id": "1042",
"@status": "paid",
"customer": "Ada Lovelace",
"item": [
{
"@sku": "A-1",
"@qty": "2",
"#text": "Notebook"
},
{
"@sku": "B-7",
"@qty": "1",
"#text": "Pen & ink"
}
],
"note": ""
}
}

The two <item> elements become an array; <note/> becomes an empty string.

A single element, with Always arrays

Input
<team><member>Grace</member></team>
Output
{
"team": {
"member": [
"Grace"
]
}
}

Without Always arrays, member is the string "Grace"; with a second <member> it would become an array. Always arrays keeps the shape stable.

Parse values

Input
<item qty="3" price="39.90" available="true">0042</item>
Output
{
"item": {
"@qty": 3,
"@price": 39.9,
"@available": true,
"#text": 42
}
}

Numbers and booleans become JSON types, but 39.90 loses its trailing zero and the code 0042 becomes 42.

Arrays and types

Whether an element becomes an array depends on how many times it appears, so the same XML schema can produce different JSON shapes from one document to the next. If code reads the result, turn on Always arrays so every element (below the root) is always an array.

Values are kept as strings by default. Parse values turns numbers and true/false into JSON numbers and booleans, but it also turns codes like 0042 into 42 and 39.90 into 39.9; use it only when that's what you want.

Common errors and how to fix them

Tags closed in the wrong order

Elements must close in the reverse order they opened. HTML parsers forgive this; XML parsers don't.

Invalid
<a><b>text</a></b>
Fixed
<a><b>text</b></a>

The tool reports: </a> closes <b>, which is still open. Close <b> first.

Options

Attribute prefix
Marks properties that came from attributes: @ (default), @_, - or _.
Text key
The property for an element's text when it also has attributes or children: #text (default), _text, $t or value.
Always arrays
Make every element below the root an array, so the JSON shape doesn't depend on how many times an element appears.
Parse values
Convert numbers and true/false to JSON types. Off by default, so values stay exactly as written.

Limitations

  • Comments, processing instructions and the XML declaration are dropped.
  • Mixed content (text between child elements) is joined into one text value.
  • Namespace prefixes are kept as part of the names (soap:Envelope).
  • Input up to a few megabytes.

Frequently asked questions

Why is an element sometimes an array and sometimes not?

JSON has no way to say "this can repeat" without seeing it repeat. Turn on Always arrays for a consistent shape.

Can I convert the JSON back to XML?

Yes, with the JSON to XML converter, which uses the same conventions: the result is the same XML, apart from comments and formatting.

Are entities like &amp; decoded?

Yes. The five XML entities and numeric character references are decoded, so &amp; becomes & in the JSON.

Is my XML uploaded?

No. The conversion runs in your browser, and nothing you paste is sent anywhere.