EthicalFusion

JSON to XML Converter

Convert a JSON object or array into well-formed, indented XML.

Was this tool helpful?

Thanks for the feedback!

Paste a JSON object and this converter produces indented XML, with an XML declaration at the top, everything wrapped in a <root> element, and each key turned into a child element. It helps developers who need to feed JSON data to a system that only accepts XML, such as an older SOAP service, an enterprise integration or a document template.

Example

Input:

{"user": {"name": "Asef", "age": 28, "tags": ["admin", "editor"],
          "manager": null, "home city": "Pune & Mumbai"}}

Output after clicking Convert to XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <user>
    <name>Asef</name>
    <age>28</age>
    <tags>admin</tags>
    <tags>editor</tags>
    <manager/>
    <home_city>Pune &amp; Mumbai</home_city>
  </user>
</root>

The mapping rules

  • Objects become elements containing one child element per key, indented two spaces per level.
  • Arrays become repeated elements that all use the key's name, as with the two tags elements. There is no wrapper element around the list.
  • Strings, numbers and booleans become the element's text, so true is written as the text true.
  • null becomes an empty self-closing element such as <manager/>.
  • Special characters in text are escaped: & becomes &amp;, and < and > become &lt; and &gt;.
  • Key names keep letters, digits, underscores, dots and hyphens; any other character, including a space, becomes an underscore, which is why "home city" turned into home_city.
  • Every value becomes an element. The converter never produces XML attributes.

Inputs that give invalid XML

A top-level array

If the JSON starts with [, each array item becomes its own <root> element, one after another. An XML document may have only one root element, so strict parsers will reject the result. Wrap the array in an object first: {"item": [ ... ]} gives a single root with repeated <item> children.

Keys that start with a digit

XML element names cannot begin with a number, but a key such as "2024" is passed through as <2024>. Rename those keys, for example to year_2024, before converting.

Empty arrays and objects

An empty array produces no element at all, and an empty object produces an opening and closing tag with blank lines between them. If the receiving system expects the element to exist, add it by hand.

Before sending the XML on

Most XML consumers validate against a schema (XSD) that fixes element names, order and sometimes a namespace. The converter keeps your JSON key order and adds no namespace, so compare the output with a sample of what the system expects and rename root if needed. Invalid JSON is reported with the parser's error message; check it in the JSON Validator first. To reformat or inspect the XML afterwards, use the XML Formatter, and for the reverse direction there is the XML to JSON Converter.

Frequently asked questions

How are JSON arrays converted to XML?
Each item becomes a repeated element named after the array's key. For example, "tags": ["a","b"] becomes two tags elements.
Why is my output wrapped in a root element?
An XML document needs one top-level element, so the converter wraps everything in root. You can rename it after converting.
Why does my XML have several root elements?
Your JSON starts with an array. Wrap it in an object such as {"item": [...]} so the output has a single root.
Can the converter create XML attributes?
No. Every key becomes a child element. Move values into attributes by hand if the target format needs them.
What happens to keys with spaces?
Spaces and other characters that are not allowed in element names are replaced with underscores, so "home city" becomes home_city.

Related tools

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.