JSON to XML Converter
Paste JSON or drop a .json file and get XML you can copy or download.
How to use
- Step 1Paste the JSON or drop a .json file. An object, an array or JSON Lines all work; a top-level array becomes repeated elements inside the root.
- Step 2Name the root element (root by default), decide what the items of arrays are called (automatic: order for orders, otherwise item) and pick the indentation. Turn the declaration off if the XML is going to be pasted into a larger document.
- Step 3Copy the XML or download data.xml. If a schema or an API expects particular element names, rename the JSON keys first: the converter only changes keys that are not legal XML names, and it lists every change.
Why JSON and XML do not map one to one
JSON has objects, arrays, strings, numbers, booleans and null. XML has elements, attributes and text. Each JSON value therefore needs a rule, and the rules this page applies mirror the conventions XML-to-JSON tools use in the other direction, so a document can travel both ways.
| JSON | XML | Note |
|---|---|---|
| {"customer": {"name": "Ada"}} | <customer><name>Ada</name></customer> | an object is an element with child elements |
| {"items": [{"sku": "A"}, {"sku": "B"}]} | <items><item><sku>A</sku></item><item>…</item></items> | an array is repeated elements; the item name is the singular of the key |
| [1, 2] | <root><item>1</item><item>2</item></root> | a top-level array is wrapped in the root |
| "paid": true, "total": 48.9 | <paid>true</paid><total>48.9</total> | numbers and booleans become text |
| "note": null | <note/> | null is an empty element |
| {"@id": 7, "#text": "Ada"} | <customer id="7">Ada</customer> | @keys are attributes, #text the element's text |
| {"first name": "Ada"} | <first_name>Ada</first_name> | keys that are not XML names are repaired, and the page says so |
Attributes, item names and the conventions other tools use
XML has attributes and JSON does not, so a convention is needed to say which keys should become attributes. This page uses the @ prefix (as the BadgerFish convention does) and #text for the text of an element that also carries attributes (as fast-xml-parser, the library behind this page, writes it). Only plain values can be attributes; an object or array under an @ key becomes a child element instead, with a note. xml2js users will recognise the same idea under the names $ and _; rename those keys to @… and #text and the output is the same.
Array items need a name, because XML has no list, only repetition. The name is the singular of the key that holds the array: orders gives order, categories gives category, boxes gives box, addresses gives address. When the key is not a plural (data, children, status) the items are called item. Set "Array items" to force one name for every array. An array directly under the root takes its name from the root element, so a root named orders gives order elements. Nested arrays become item elements inside item elements.
Names, escaping and what changes on the way
- An XML name may contain letters, digits, underscores, hyphens, dots and colons and may not start with a digit, a hyphen or a dot. A key such as first name becomes first_name, 2nd becomes _2nd, an empty key becomes _. Two keys that repair to the same name become sibling elements; nothing is silently dropped.
- The five characters < > & " ' are written as entities in text and attribute values, so any string is safe. Line breaks inside strings are kept as they are.
- Numbers are re-printed the way JavaScript prints them: 1.0 becomes 1 and very large values use exponent notation. Quote a number in the JSON ("1.0") to keep its exact text.
- Key order is preserved, which matters for XML where order is significant. The declaration <?xml version="1.0" encoding="UTF-8"?> is added by default; indentation is two spaces, four spaces or a tab, and empty elements are written as <a/>.
- Namespaces are not invented: a key ns:tag is written as is, and a declaration is only added if you put it in the JSON yourself as "@xmlns:ns": "urn:example".
Questions and answers
How are the items of an array named?
By the singular of the key that holds the array (orders → order, entries → entry), or item when the key is not a plural. Type a name under "Array items" to use it for every array in the document.
Can some values become attributes instead of elements?
Yes. Start the key with @ and keep the value a string, number, boolean or null: {"@id": 7, "name": "Ada"} gives <customer id="7"><name>Ada</name></customer>. Use #text for the text of an element that has only attributes beside it. With "@keys as attributes" off, those keys become ordinary elements named id and text.
Why was one of my keys renamed?
Because it is not a legal XML element name: it contains a space or punctuation, or starts with a digit. The warning under the output lists each key and the name it received. If a schema requires an exact name, change the key in the JSON, for example to firstName or first_name, and convert again.
Will the XML validate against my schema (XSD or DTD)?
No schema is applied: the structure follows the JSON, values are plain text and elements come in key order. If the JSON mirrors what the schema expects, the result usually validates; check with a validator such as xmllint --schema before sending it to a system that enforces one.
What happens to null, true and false?
null becomes an empty element such as <note/>. true and false are written as the words true and false, and numbers as their JSON text, because XML has no types of its own; whatever reads the XML decides how to interpret them.