Convert JSON to XML
Paste JSON or drop a .json file, and the XML appears as you type. The conversion runs in this tab, so nothing you paste is sent anywhere.
How to use it#
- Drop a .json file on the box, or paste the JSON into it.
- Choose indented or minified XML, and whether it starts with an XML declaration.
- Copy the XML, or download it as an .xml file named after the one you dropped.
How the JSON becomes XML#
Every key becomes an element holding its value, and nested objects become nested elements. An array is written the way XML writes a list, as the same element repeated, so "tag": ["a", "b"] becomes two <tag> elements side by side. Null and an empty string both become an empty element, <name />. Numbers are copied as written, so a price of 1.50 keeps its zero and a 20 digit ID keeps every digit.
Two kinds of key are read specially, to give you control over the parts of XML that JSON lacks. A key starting with @ becomes an attribute on its parent, so {"book": {"@id": 7}} gives <book id="7" />. A key named #text becomes the element's own text, beside its attributes. These are the same keys the XML to JSON converter writes, so XML that was converted to JSON comes back here as the XML it started as.
Where the root element comes from#
An XML document has exactly one top-level element. When the JSON is an object with a single key holding an object or a value, such as {"order": {...}}, that key is the root. Anything else, an object with several keys, a single key holding a list, a list, or a single value, is wrapped in an element called root, and the page says so under the result.
A top-level array becomes <root> holding one <item> per entry. An array nested directly inside another array gets the same treatment, because XML has no way to write a list of lists without naming the inner ones.
Keys that cannot be XML names#
JSON allows any string as a key, and XML names are much stricter. A name cannot start with a digit or contain spaces, and most punctuation is out. Such a key is changed rather than refused: spaces and punctuation become underscores, and a name starting with a digit gets an underscore in front, so first name becomes first_name and 2024 becomes _2024. The page lists every key it had to change.
The change does not reverse on the way back, so if the XML is going to be read by code that expects the original names, fix the keys in the JSON first. Elements come out in the order their keys were written, and a key that appears twice in one object becomes two elements, since XML can repeat an element, where most JSON readers keep only the last value.
Frequently asked questions#
How do I make an attribute instead of an element?
#
Start the key with @. {"link": {"@href": "/about", "#text": "About us"}} becomes <link href="/about">About us</link>. The value of an @ key should be a string, a number, or a boolean; anything else is written as its JSON text, since an attribute can only hold text.
Does it add an XML declaration?
#
By default, yes: the output starts with <?xml version="1.0" encoding="UTF-8"?>, which tells a reader the file is UTF-8. Untick the option to leave it off, which some systems expect when the XML is embedded in another document.
Will the XML convert back to the same JSON?
#
Mostly, and the differences are predictable. JSON that needed a root element added comes back inside it, under a root key. An element that appears once comes back as a single value rather than an array of one, and an empty array writes nothing, so it does not come back at all. Numbers and booleans return as numbers and booleans when the XML to JSON converter has type conversion on.
Is my JSON uploaded?
#
No. The conversion is plain JavaScript running in this tab.