StringMash.com

JSON to XML

Turn keys into elements, with a root you choose and text that is escaped.

Conversion
50 characters
Updates as you type
Root taken from the JSON

Using the converter

Paste JSON on the left and XML appears on the right. A key beginning with @ is written as an attribute, a key called #text becomes the element's own text, and an array repeats its element.

Root element is used when the JSON needs a wrapper. If your JSON has exactly one key at the top, that key is the root and the setting is ignored, since inventing a second wrapper round it would change the shape for no reason.

XML needs exactly one root

This is the constraint that shapes everything else. An XML document has one outermost element, always. JSON has no such rule: a file can be an array, a number or a bare string, and plenty are.

So a wrapper sometimes has to be invented, and an invented name is a decision the converter is making on your behalf. The setting exists so it is your decision instead.

Names have rules too. An XML element name cannot start with a digit and cannot contain a space, so a JSON key like "first name" or "2024" is rewritten into something legal. Check the output if your keys came from a spreadsheet header.

Good, not Evil

The JSON licence, written by Douglas Crockford for his own Java implementation, is the MIT licence with one sentence added: "The Software shall be used for Good, not Evil."

It caused real trouble. The clause makes the licence non-free by the usual definitions, because it restricts the field of use, so Debian and the Free Software Foundation both treat it as unusable. Apache told its projects to stop shipping the code. Companies wrote to Crockford asking for an exemption, and he has said IBM's lawyers got a letter back giving their customers permission to use it for evil.

The clause is still there. JSON itself carries no such condition, since a data format cannot be licensed; only Crockford's implementation of it can.

Related conversions

XML to JSON is the other direction, and explains why no single mapping between the two is correct. JSON to YAML is a gentler reformatting, since YAML can already hold everything JSON can.

To flatten a list of objects into rows instead, use JSON to CSV.

Questions

How do I convert JSON to XML?

Paste the JSON above. Keys become elements, a key starting with @ becomes an attribute, and arrays repeat their element.

How is the root element chosen?

If the JSON has exactly one key at the top level, that key is the root. Otherwise a wrapper is added, and the Root element setting names it.

What happens to a key that is not a valid element name?

It is rewritten. XML names cannot begin with a digit or contain spaces, so "first name" becomes first_name.

Are special characters escaped?

Yes. Ampersands and angle brackets are escaped in text, and quotes are escaped inside attribute values.