JSON to XML Converter
Developer Tools · Added
JSON and XML do not describe the same things — XML has attributes, ordered mixed content and exactly one root, JSON has arrays and unordered keys — so every converter has to pick a convention, and one that does not say which is producing output you cannot rely on. This uses the attribute-prefix form, states it plainly, and inverts the same rules going back the other way so a document survives the round trip.
How to use the json to xml converter
- 1Pick the direction: JSON to XML, or XML to JSON.
- 2Paste your document, or load the example to see how the convention maps.
- 3Going to XML, set the root element name — it is used only when the JSON does not already have a single top-level key.
- 4Coming back to JSON, decide whether numeric-looking text should become numbers. It is off by default, because a leading zero is significant in a part number.
Examples
Attributes and children together
- Input
- { "order": { "@id": "A-1042", "customer": "Meera Nair" } }
- Result
- <order id="A-1042"><customer>Meera Nair</customer></order>
A key beginning with "@" becomes an attribute on the element that contains it.
An array becomes repeated elements
- Input
- { "list": { "item": ["one", "two"] } }
- Result
- <list><item>one</item><item>two</item></list>
XML has no concept of a list, so repetition is the only way to express one.
Reading XML back
- Input
- <book isbn="978-0131103627"><year>1978</year></book>
- Result
- { "book": { "@isbn": "978-0131103627", "year": "1978" } }
The year stays a string unless type coercion is switched on, because XML never says which it was.
About the json to xml converter
The mapping, in full
An object key becomes an element name. A key starting with @ becomes an attribute of the enclosing element. The key #text becomes the element's text content, which is how an element that has both attributes and text is expressed. An array repeats the parent's element name once per item. Null becomes an empty element. Going the other way, each of those rules is inverted, and a repeated element name collapses into an array.
That last rule has a consequence worth planning for. Whether a key comes back as an array depends on how many elements happened to be present, so a list with one item in it reads back as a single object rather than an array of one. Anything consuming the JSON should normalise that case rather than assuming the shape — it is the single most common source of bugs when moving data between the two formats.
Escaping, which is where hand-written converters break
Text going into XML has to have its ampersands, less-thans and greater-thans replaced with entities, and attribute values need their quotes escaped as well. Miss one and the document is not merely wrong, it is unparseable — a stray & in a URL inside an element is enough to break the whole file. Coming back, entities have to be decoded, including numeric ones in both decimal and hexadecimal form, and CDATA sections must be taken literally rather than decoded again.
All of that is handled here, and it is the practical reason to use a converter rather than assembling XML with string concatenation. The output is also indented rather than emitted on one line, since a document that a person has to read is worth more than four saved bytes.
Which format to use, if you have the choice
For a new API, JSON, almost always — it maps directly onto the data structures of every language people write clients in, and it carries no ceremony. XML earns its keep where documents rather than data are the subject: mixed content where markup is interleaved with prose, schema validation that has to be enforced by a third party, digital signatures over part of a document, and the many established formats built on it, from RSS and SVG to invoicing standards that industries have standardised on.
Most conversions are not a choice between them at all, but a bridge: an established system speaks one and a new one speaks the other. In that situation the important thing is not which format is better, but that the mapping is explicit, documented and reversible — which is what the convention on this page is for.
Frequently asked questions
Why does the converter need a convention at all?
What happens to a JSON key that is not a legal XML element name?
Why is type conversion off when reading XML?
Is anything lost converting XML to JSON?
Does my data leave the browser?
Related tools
JSON to YAML Converter
Developer Tools
Convert JSON to YAML or YAML back to JSON, with the ambiguous values quoted.
XML Formatter
Developer Tools
Indent or minify XML and check it is well-formed, without disturbing mixed content or CDATA.
CSV to JSON Converter
Developer Tools
Convert CSV to JSON or JSON back to CSV, with proper quoted-field handling.
ASCII Table
Developer Tools
Searchable ASCII reference with decimal, hex, octal, binary and escapes.