XML to JSON Converter
Paste XML or open a file to get equivalent JSON. The conversion runs entirely in your browser, so your data is never uploaded.
Swap to JSON to XMLPaste text, or drop a file here
Example
XML input
<?xml version="1.0" encoding="UTF-8"?>
<library city="London">
<book id="1"><title>Notes</title><year>1843</year></book>
<book id="2"><title>Compilers & Code</title><year>1952</year></book>
</library>JSON
{
"library": {
"book": [
{
"title": "Notes",
"year": 1843,
"@id": 1
},
{
"title": "Compilers & Code",
"year": 1952,
"@id": 2
}
],
"@city": "London"
}
}How it handles tricky cases
Attributes
An attribute becomes a key with an @ prefix, so id="1" becomes "@id": 1. When an element has both attributes and text, the text goes in #text.
Repeated elements
Elements that repeat become an array. An element that appears only once stays a single value, so code reading the JSON should allow for both shapes.
Namespaces
Prefixes stay in the key name, so soap:Body becomes the key soap:Body.
What isn't kept
XML comments and processing instructions are dropped. CDATA becomes normal text. In mixed content, where text and child elements alternate, their relative order is not preserved.
Invalid XML
The document is validated first, and the error message gives the line and column of the problem.
Do it from code
Python with xmltodict:
import json, xmltodict
with open("in.xml", encoding="utf-8") as f:
data = xmltodict.parse(f.read())
print(json.dumps(data, indent=2))
Questions
Why did my number stay text?
Values with leading zeros stay text so that codes like 007 aren't changed. You can also turn off detection so every value stays text.
Is there one standard way to map XML to JSON?
No. Tools differ on attributes and arrays. This one uses the @ prefix for attributes and #text for element text.
Is my XML uploaded?
No. It is parsed on your device.