JSON to XML converter
Paste or open a JSON file and the XML shows up on the right. Copy it, or download it as an .xml file. Nothing leaves your browser.
JSON
XML
How the JSON is turned into XML
Each key becomes an element with the value inside it. A few keys have special meanings, so you can control the XML you get:
{
"order": {
"@id": "1042",
"customer": "Maya Chen",
"items": {
"item": [
{ "@sku": "MUG-01", "name": "Ceramic mug" },
{ "@sku": "TEA-12", "name": "Green tea" }
]
},
"paid": true,
"giftNote": null
}
}<?xml version="1.0" encoding="UTF-8"?>
<order id="1042">
<customer>Maya Chen</customer>
<items>
<item sku="MUG-01">
<name>Ceramic mug</name>
</item>
<item sku="TEA-12">
<name>Green tea</name>
</item>
</items>
<paid>true</paid>
<giftNote/>
</order>- A key that starts with
@becomes an attribute, so"@id"is written asid="1042". - An array repeats its key once per item, so
itemappears twice. - A
"#text"key holds an element’s text when it also has attributes, as in{ "@currency": "USD", "#text": "24.50" }. nulland empty strings become empty elements, like<giftNote/>. Numbers andtrue/falseare written as text, exactly as they appear in the JSON.
These are the same rules the XML to JSON converter uses, so XML converted to JSON and back comes out the same, apart from comments.
The root element
XML needs exactly one outer element. If your JSON is an object with a single key, like order above, that key becomes the root. Anything else is wrapped in <root>: an object with several keys, a top-level array (each item becomes an <item>), or a single value. To choose the name yourself, put your JSON inside an object with that one key.
Arrays don’t get a wrapper
An array becomes a series of elements with the same name, without an element around them:
{ "tags": ["api", "json"] }<root>
<tags>api</tags>
<tags>json</tags>
</root>If the XML you need has a wrapper, like <tags><tag>api</tag></tags>, nest the array in an object: { "tags": { "tag": ["api", "json"] } }. That’s what items does in the first example. An empty array produces no elements at all.
Keys that can’t be tag names
XML tag names can’t contain spaces, can’t start with a digit, and can’t start with “xml”. JSON keys can be anything, so keys like "first name" or "2024" are changed to first_name and _2024, and the tool lists every key it changed. Characters like & and < in values are escaped for you.
Doing it in code
# Python, with xmltodict (pip install xmltodict)
import json, xmltodict
with open("order.json") as f:
print(xmltodict.unparse(json.load(f), pretty=True))
// JavaScript, with fast-xml-parser (npm install fast-xml-parser)
import { XMLBuilder } from "fast-xml-parser";
const builder = new XMLBuilder({ ignoreAttributes: false, attributeNamePrefix: "@", format: true });
const xml = builder.build(JSON.parse(text));
# Command line, with yq
yq -p json -o xml order.jsonxmltodict uses the same @ and #text keys as this page, and the single top-level key becomes the root element, the same as here. fast-xml-parser expects attributes as @_id unless you set attributeNamePrefix, as above.
Files can be up to 2 MB. If the JSON won’t parse, the error shows you the line, or you can run it through the JSON validator first. Going the other way? Use XML to JSON.