How to Convert XML to JSON
XML is verbose to work with in modern code, so converting it to JSON makes it much easier to handle in JavaScript and most APIs. The one thing to understand is how XML's extra features — attributes and mixed content — map onto JSON, which doesn't have them.
The basic mapping
Each element becomes a key. An element with only text becomes a string value:
<note><to>Dev</to><from>DevUtilsNow</from></note>
{
"note": {
"to": "Dev",
"from": "DevUtilsNow"
}
}
Attributes and repeated elements
Two XML features need a convention in JSON:
- Attributes are kept as keys prefixed with
@, so they don't collide with child element names —<note id="1">gives"@id": "1". - Repeated elements with the same name collapse into an array, in order:
<tags><tag>fast</tag><tag>free</tag></tags>
{ "tags": { "tag": ["fast", "free"] } }
When an element has both text and attributes, the text is stored under #text.
Why values stay strings
XML has no types — <count>42</count> is text, not a number. Rather than guess (and risk turning a ZIP code like "01234" into 1234), the converter keeps every value as a string. Convert to numbers or booleans in your own code where you know the intent.
The other direction
Need to go back? See How to Convert JSON to XML, and to format raw XML first, try the XML Formatter.
Try it
Paste XML and get structured JSON instantly — everything runs in your browser, nothing uploaded.