What JSON to XML does
JSON to XML reads JSON and writes it as XML. The input is parsed first, so a syntax error is reported with its position instead of producing broken output.
JSON to XML examples
The built-in sample
Input
{"name":"Ada Lovelace","active":true,"tags":["math","code"],"address":{"city":"London"}}Output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>Ada Lovelace</name>
<active>true</active>
<tags>math</tags>
<tags>code</tags>
<address>
<city>London</city>
</address>
</root>This is the input that the Load sample button fills in.
Attributes, text and names that XML does not allow
Input
{"order":{"@id":"7","item":[{"@sku":"A1","#text":"Pen"},{"@sku":"B2","#text":"Ink & paper"}],"note":null,"ship to":"Bengaluru"}}Output
<?xml version="1.0" encoding="UTF-8"?>
<order id="7">
<item sku="A1">Pen</item>
<item sku="B2">Ink & paper</item>
<note/>
<item>Bengaluru</item>
</order>The single top-level key becomes the root element, @id and @sku become attributes, the ampersand is escaped and null becomes an empty element. "ship to" contains a space, so it cannot be an element name and is written as item: rename such keys first if the name matters.
Each output was produced by running this tool on the input above it.
How it works
JSON is defined by RFC 8259 and has six value types. XML, defined by the W3C XML 1.0 Recommendation, has elements, attributes and text. The converter parses your JSON, then writes one element per key. XML needs a single root element, so an object with exactly one top-level key (whose value is not an array) uses that key as the root. Anything else, including a top-level array, is wrapped in one root element, with an item element per array entry.
Keys that are not valid XML names, such as keys with spaces or a leading digit, are written as item so the output always stays well-formed. Text is escaped, so & and < never break the document.
Data mapping rules
| JSON | XML output | Note |
|---|---|---|
| {"a":"x"} | <a>x</a> | A key becomes an element |
| {"a":[1,2]} | <root><a>1</a><a>2</a></root> | Arrays repeat the element once per item |
| [1,2] | <root><item>1</item><item>2</item></root> | A top-level array becomes item elements |
| {"a":{"@id":"1","#text":"x"}} | <a id="1">x</a> | Keys starting with @ become attributes, #text becomes text |
| {"a":null} | <a/> | null becomes an empty element |
| {"a":"x & y"} | <a>x & y</a> | Special characters are escaped; CDATA is not generated |
XML has no native types, so numbers, booleans and null lose their type when written as text. A system that reads the XML must know the types again.
Details and limits
- There is no fixed size limit: everything runs on your device, so very large inputs depend on its memory.
- It works in current browsers on phones, tablets and computers, and it is free with no account needed.
- Values are carried across without losing anything in ordinary data; the points above list the cases the two formats handle differently.
- An array has no element of its own: its items repeat the name of the key that holds the array. An empty array therefore produces nothing at all.
- The output always starts with an XML declaration for UTF-8.
- Numbers and booleans are written as text, so the conversion back with XML to JSON returns strings.
- Input that is not valid JSON stops the tool, and the status line shows where the problem is. The JSON Validator page lists the usual causes.
Questions
How do I get attributes in the XML?
Prefix the key with @, for example {"item":{"@id":"1","#text":"Pen"}} gives <item id="1">Pen</item>.