Home JSON to XML

JSON to XML converter

Paste JSON and get indented, well-formed XML back. JSON and XML do not map one to one, so the rules are written out below — and they are the mirror image of this site’s XML to JSON converter.

Other languages:TürkçeDeutsch中文हिन्दीРусский

The mapping, rule by rule

XML has elements, attributes and text; JSON has objects, arrays and typed values. Every JSON-to-XML converter picks a mapping, and most do not say which. This one uses the same conventions its XML to JSON counterpart produces, so a document can travel in both directions:

JSONXML
{"order": {…}} (one top-level key)the key is the root element: <order>…</order>
several top-level keys, or a top-level arraywrapped in the root name you choose (root by default); array items become <item>
"item": [{…}, {…}]the element repeats: <item>…</item><item>…</item>
"@id": "1001"an attribute: <order id="1001">
"#text": "hi"the element's text content
nullan empty element: <note/>
true, 42text: <paid>true</paid>

Keys that are not XML names

A JSON key can be any string; an XML element name cannot contain spaces, cannot start with a digit, a hyphen or a full stop, and cannot start with xml. Rather than emit XML that no parser will read, illegal characters become _ and a name that starts badly gets a _ prefix: "gift wrap" becomes <gift_wrap> and "1st" becomes <_1st>. If a consumer needs the original key, keep it as an attribute instead.

What is lost on the way

XML text has no types. 42 and "42" produce the same element, as do null and "", so the trip back to JSON cannot know which you meant — that is a property of XML, and a schema (XSD) is the usual way to put the types back. An empty array produces no element at all, because zero repetitions of an element is nothing. And a one-item array produces one element, indistinguishable from a single object.

Characters that XML 1.0 cannot contain — most control characters — are replaced with U+FFFD; &, < and > are escaped, and so are quotes inside attributes. Numbers are written exactly as they appear in the JSON, because the input is read with an exact parser rather than JSON.parse.

When XML is the right target

Mostly when something old or strict is on the other end: a SOAP service, an RSS or sitemap consumer, a bank or government integration, a test harness that only reads XML fixtures. For generating XML test records from scratch rather than converting a file, the XML export writes them directly from a schema.

Common questions

How do I make an XML attribute from JSON?

Prefix the key with @. {"order": {"@id": "1001"}} becomes <order id="1001">. Attribute values must be plain values — an object under an @ key is written as a child element instead.

How are JSON arrays converted?

An array repeats the element it belongs to, once per item: "item": [a, b] becomes two <item> elements. A top-level array is wrapped in the root element, with each entry as <item>.

What root element is used?

If the JSON is an object with a single key, that key is the root. Otherwise everything is wrapped in the root element name in the options, which defaults to root.

What happens to keys with spaces?

They are repaired into legal XML names: spaces and other illegal characters become underscores, and a name that starts with a digit or with xml gets an underscore prefix.

Is the conversion reversible?

Structurally, through the XML to JSON converter, which uses the same @ and #text conventions. Types are not: XML text does not distinguish 42 from "42" or null from an empty string.

Last updated