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 declaration adds <?xml version="1.0" encoding="UTF-8"?> as the first line. It's off by default. Some older parsers and feeds expect it.

XML needs exactly one root

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. Root element is where you name it, and if you leave it alone it's called root. A top-level array goes inside it, one <item> per entry. An empty array writes nothing at all.

Names have rules too. An XML element name can't start with a digit or contain a space, so a JSON key like "first name" or "2024" is rewritten into something legal. Attribute names get the same treatment. Check the output if your keys came from a spreadsheet header.

Most control characters can't appear in XML at all, not even escaped. Any in your strings come out as the replacement character �.

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. IBM's lawyers asked Crockford for an exemption on JSLint, which carried the same clause, and in a 2011 talk he said he'd written back giving "IBM, its customers, partners, and minions" permission to use it for evil.

It lasted 20 years. In 2022 the Java library went into the public domain, and Apache moved it back to its allowed list. JSON itself never carried the condition.

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 isn't a valid element name?

It's rewritten. XML names can't 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.

Sources

Added . Last updated and reviewed . What's new