How to convert JSON to XML
- Paste JSON into the left box, drop a .json file on it, or press Upload. JSON errors are reported with their line and column; format and validate the JSON first if it came from a messy source.
- The XML appears on the right as you type, indented with 2 spaces.
- Adjust the attribute prefix, text key, root and array item names if your JSON uses different conventions, and choose whether to add the
<?xml?>declaration. - Copy the XML or Download .xml.
How JSON maps to XML
| JSON | XML |
|---|---|
"title": "Dune" | <title>Dune</title> |
"tag": ["a", "b"] | <tag>a</tag><tag>b</tag> |
"@_id": "b1" | attribute id="b1" on the parent element |
"#text": "9.5" | the text inside the parent element |
null or "" | an empty element, <note/> |
true, 42 | text: true, 42 |
This is the same convention the XML to JSON converter uses, so you can convert XML to JSON, edit it, and convert it back.
JSON to XML example
An order with attributes, a repeated element and a text-plus-attribute value:
{
"order": {
"@_id": "A-17",
"customer": "Ada Lovelace",
"item": [
{ "@_sku": "P1", "name": "Fountain pen", "qty": 1 },
{ "@_sku": "I9", "name": "Ink & cartridges", "qty": 3 }
],
"total": { "@_currency": "USD", "#text": 42.5 },
"gift": true,
"note": null
}
}converts to:
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-17">
<customer>Ada Lovelace</customer>
<item sku="P1">
<name>Fountain pen</name>
<qty>1</qty>
</item>
<item sku="I9">
<name>Ink & cartridges</name>
<qty>3</qty>
</item>
<total currency="USD">42.5</total>
<gift>true</gift>
<note/>
</order>
The & in the item name is escaped as &, null became <note/>, and the two items became two <item> elements.
Root element and top-level arrays
An XML document must have a single root element. When the JSON is an object with one key, that key is used; otherwise the converter adds a root. A top-level array is wrapped in the root, with one element per item. With Root set to products and Array item set to product:
[
{ "id": 1, "name": "Pen" },
{ "id": 2, "name": "Ink" }
]<products>
<product>
<id>1</id>
<name>Pen</name>
</product>
<product>
<id>2</id>
<name>Ink</name>
</product>
</products>
Keys that are not valid XML names
JSON keys can be any string, but XML names can't start with a digit or contain spaces and most punctuation. Invalid characters become underscores, and names that start with a digit get a leading underscore:
{ "user": { "first name": "Ada", "2fa": true, "e-mail": "[email protected]" } }<user>
<first_name>Ada</first_name>
<_2fa>true</_2fa>
<e-mail>[email protected]</e-mail>
</user>
Hyphens, dots and namespace prefixes such as dc:title are allowed and kept as they are.
Escaping and special characters
Text is escaped so the XML is always well-formed: & → &, < → <, > → >, and double quotes in attribute values → ". Accented letters and emoji are written as they are, since the document is UTF-8. Control characters that XML 1.0 forbids are removed, and the status line tells you when that happens.
Working with a large, deeply nested JSON file? Browse it in the JSON viewer first to decide which parts to keep.
Frequently asked questions
How do I convert JSON to XML?
Paste the JSON into the left box or upload a .json file. The converter builds indented XML as you type: each key becomes an element, and arrays become repeated elements. Press Copy, or Download .xml to save the file.
How do I create XML attributes from JSON?
Start the key with the attribute prefix, @_ by default. {"book": {"@_id": "b1", "title": "Dune"}} becomes <book id="b1"><title>Dune</title></book>. You can change the prefix, for example to @ or -, in the options.
How do I put text and attributes on the same element?
Use the text key, #text by default, next to the attribute keys: {"price": {"@_currency": "USD", "#text": 9.5}} becomes <price currency="USD">9.5</price>.
What is the root element?
XML needs exactly one root element. If your JSON is an object with one key (like {"order": {...}}), that key is the root. Otherwise everything is wrapped in <root>, and you can rename it in the options.
How are JSON arrays converted to XML?
An array under a key repeats that element once per item: "tag": ["a", "b"] becomes <tag>a</tag><tag>b</tag>. An array at the top level, or an array inside an array, is wrapped in <item> elements, and you can rename item too.
What happens to null, true/false and empty strings?
null and empty strings become empty elements such as <note/>. Numbers and true/false are written as text, because XML has no data types without a schema.
Why was one of my keys renamed?
XML element names cannot contain spaces, cannot start with a digit and allow only a few punctuation characters. The converter replaces invalid characters with an underscore and adds _ in front of names that start with a digit, and the status line lists the keys it changed.
Can every JSON document be converted to XML?
Yes, with the conventions above, and the result is always well-formed XML. Special characters are escaped (& becomes &, < becomes <), and control characters that XML 1.0 does not allow are removed with a warning.
Is my JSON uploaded?
No. The XML is built in your browser, and nothing is sent to a server or stored. Only your option choices are remembered on your device.