JSON to XML Converter

Paste JSON and get indented XML. DevPocket turns prefixed keys into attributes and arrays into repeated elements, runs in your browser, and explains what XML cannot hold.

Loading the tool. It runs entirely in your browser...

How JSON maps to XML

A key becomes an element. A key that starts with the attribute prefix (@ by default) becomes an attribute of its parent, and the text key (#text) becomes the element's text, so the same settings read back what XML to JSON writes. An array becomes the same element repeated, a number or boolean is written as it appears, and null or an empty string becomes an empty element.

XML needs one root. A single-key object whose value is not an array supplies it; otherwise the JSON is wrapped in the root name you set. An array inside an array uses item as the inner element name. The characters & and < are escaped, and quotes inside attributes too.

It refuses input XML cannot hold, with the path: a key that is not a valid element name (first name, 1st), an attribute whose value is an object or array, and control characters that XML 1.0 forbids. Nothing is renamed behind your back.

Worked example

Input:

{"order": {"@id": "1001", "@status": "paid", "customer": "Ann & Co",
  "item": [{"@sku": "A1", "@qty": 2, "#text": "Widget"},
           {"@sku": "B2", "@qty": 1, "#text": "Gadget"}],
  "gift": null}}

Output:

<?xml version="1.0" encoding="UTF-8"?>
<order id="1001" status="paid">
  <customer>Ann &amp; Co</customer>
  <item sku="A1" qty="2">Widget</item>
  <item sku="B2" qty="1">Gadget</item>
  <gift/>
</order>

The number 2 became the attribute text "2", and the null gift became an empty element.

Round trips and limits

  • XML has no types: numbers, booleans and strings all become text, so reading it back gives strings unless you convert them.
  • null and "" both give an empty element, which reads back as an empty string.
  • Text next to child elements is written first, so the order between them is not kept.
  • An empty array writes no element, and the notes say where.
  • Check broken input with the JSON formatter or JSON repair first.

Frequently asked questions

How do I make an attribute?

Start the key with the attribute prefix, @ by default, for example "@id": "7". You can change the prefix in the options.

Why is my JSON wrapped in a root element?

XML allows one root. If the JSON is an array, a scalar or an object with several keys, it is wrapped in the root name you set.

Why did it reject my key?

XML element names cannot contain spaces or start with a digit. The error names the path so you can rename the key.

Are special characters escaped?

Yes. Ampersands and angle brackets are escaped in text, and quotes and line breaks in attributes.

Will the XML read back to the same JSON?

Only partly. Types are lost, and null comes back as an empty string. The limits section lists the cases.

Is my JSON uploaded?

No. The conversion runs in your browser and nothing leaves your device.