XML to JSON converter
Paste XML or open an .xml file, and the JSON shows up on the right. If the XML has an error, you’ll see the line and column. It runs in your browser, so the file isn’t uploaded.
XML
JSON
How the XML is turned into JSON
XML has things JSON doesn’t, like attributes and repeated tags, so every converter has to pick a way to write them. This one uses the same rules as Python’s xmltodict, which is the most common choice:
<order id="1042" status="paid">
<customer>Maya Chen</customer>
<item sku="MUG-01">Ceramic mug</item>
<item sku="TEA-12">Green tea</item>
<total currency="USD">24.50</total>
<giftNote/>
</order>{
"order": {
"@id": "1042",
"@status": "paid",
"customer": "Maya Chen",
"item": [
{ "@sku": "MUG-01", "#text": "Ceramic mug" },
{ "@sku": "TEA-12", "#text": "Green tea" }
],
"total": { "@currency": "USD", "#text": "24.50" },
"giftNote": ""
}
}- The outer element becomes the only key at the top, here
order. - Attributes get an
@in front of their name, soid="1042"becomes"@id". - A tag that appears more than once, like
item, becomes an array. - An element with only text becomes a plain string. If it also has attributes or child elements, the text goes in
"#text". - An empty element such as
<giftNote/>becomes an empty string.
One item or a list
The XML doesn’t say whether item is meant to be a list. The converter can only tell from how many there are, so an order with a single item gives you an object instead of an array:
// <order><item sku="MUG-01">Ceramic mug</item></order>
{ "order": { "item": { "@sku": "MUG-01", "#text": "Ceramic mug" } } }Code that loops over order.item works for the first example and breaks on this one. If you convert XML in your own code, tell the library which elements are always lists. In xmltodict that’s force_list, shown below. If a Java app reads the JSON with Jackson and fails on a single item, see Cannot deserialize value of type.
Why numbers come out as strings
In XML, <qty>2</qty> is just text. Only a schema can say it’s a number, so the converter keeps "2" as a string rather than guessing. Guessing goes wrong with values like ZIP codes and IDs: 02108 would lose its leading zero, and a 20-digit order number would be rounded. Convert the fields you need in your own code, where you know what each one is.
What gets left out
Comments, the <?xml?> declaration and any DOCTYPE are dropped, since they aren’t data. Namespace prefixes are kept as part of the name, so a SOAP response gives keys like "soap:Body", and the xmlns declarations become attributes. Text inside <![CDATA[ ]]> is read as ordinary text.
XML that mixes text and tags, like <p>Hello <b>there</b> friend</p>, doesn’t fit JSON well. The text pieces are joined into "#text" and the order of text and tags is lost. That’s usually fine for data feeds, but not for documents like XHTML.
Common XML errors
The most common is a bare &, as in AT&T. In XML it has to be written &. HTML entities such as and © aren’t defined in XML either, unless the DOCTYPE declares them, so use the character itself or a number like  .
After that come closing tags that don’t match, like <Item>…</item> (XML is case-sensitive), attribute values without quotes, and more than one element at the top level. XML needs a single outer element around everything.
Doing it in code
# Python, with xmltodict (pip install xmltodict)
import json, xmltodict
with open("order.xml") as f:
data = xmltodict.parse(f.read(), force_list=("item",))
print(json.dumps(data, indent=2))
// JavaScript, with fast-xml-parser (npm install fast-xml-parser)
import { XMLParser } from "fast-xml-parser";
const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "@" });
const json = JSON.stringify(parser.parse(xml), null, 2);
# Command line, with yq
yq -p xml -o json order.xmlfast-xml-parser ignores attributes unless you set ignoreAttributes: false, and by default it names them @_id rather than @id. yq puts a + in front of attribute names. Check which style the code that reads your JSON expects.
Files can be up to 2 MB. To browse or query the result, open it in the JSON viewer. To convert back, use JSON to XML.