200 tools

Code & JSON Tools · · 4 min read

How to Convert JSON to XML (With Custom Tag Names)

Use the JSON to XML tool to turn JSON into well-formed XML — set a root and item tag, toggle the XML declaration, and choose 2, 4, or tab indent.

To convert JSON to XML, paste your JSON into JSON to XML — it builds well-formed XML instantly, naming the root tag after your JSON's top-level key when there is just one, wrapping arrays in a tag you can rename, and nothing ever leaves your browser.

What this tool does and why it matters

JSON and XML model data differently: JSON has no native idea of a "root element," and no way to tell a converter what to call repeated items in an array. A naive JSON-to-XML converter either forces every document under a generic <root> tag or breaks on arrays entirely. Toolbench's JSON to XML tool handles both cases sensibly: if your JSON is a single object with exactly one top-level key, that key becomes the XML root tag automatically (so {"user": {...}} becomes <user>...</user>, not <root><user>...</user></root>). Everything else — a multi-key object, an array, or a bare value — uses the Root element name you choose.

How to convert JSON to XML with Toolbench

  1. Open JSON to XML.
  2. Paste your JSON into the input panel.
  3. Set Root element (default root) — the tag name used when your JSON isn't a single-key object (arrays, multi-key objects, and bare values all use this).
  4. Set Array item element (default item) — the tag name given to each entry when your top-level JSON is an array.
  5. Leave XML declaration on (the default) to prepend <?xml version="1.0" encoding="UTF-8"?>, or turn it off for a bare XML fragment.
  6. Pick Indent: 2 spaces (default), 4 spaces, or Tab.
  7. Copy the resulting XML, or use ↑ Use as input to send it on to another transform.

Example: a single-key object (the default)

This input:

{"user":{"name":"Ana","role":"Designer"}}

produces:

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <name>Ana</name>
  <role>Designer</role>
</user>

Because the top level is an object with exactly one key (user), that key becomes the root tag — the Root element setting is ignored here. If the object had two or more top-level keys instead, like {"name":"Ana","role":"Designer"}, there'd be no single key to promote, so it would wrap in <root> (or whatever you set Root element to).

Example: an array of objects with custom tag names

Arrays are wrapped in Root element with each entry tagged Array item element. With Root element set to users and Array item element set to user, this input:

[{"name":"Ana"},{"name":"Bo"}]

produces:

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user>
    <name>Ana</name>
  </user>
  <user>
    <name>Bo</name>
  </user>
</users>

With the defaults left untouched, the same array instead wraps in <root> with each entry as <item>.

Reference: options

Option Default What it does
Root element root Tag name for the document root, used for arrays, multi-key objects, and bare values
Array item element item Tag name for each entry of a top-level array
XML declaration On Adds <?xml version="1.0" encoding="UTF-8"?> before the document
Indent 2 spaces 2 spaces, 4 spaces, or a tab per nesting level

Tips and common mistakes

  • A single-key top-level object skips the Root element setting entirely. {"product": {...}} always becomes <product>...</product> — change the key name in your JSON if you want a different root tag, since the option won't override it.
  • Nested arrays repeat the parent key, not the Array item element. The Array item element option only applies to a top-level array. An array nested inside an object reuses that object's key as the tag name for every entry — {"product":{"name":"Mug","tags":["ceramic","blue"]}} becomes a <product> with two sibling <tags> tags (<tags>ceramic</tags> and <tags>blue</tags>), not a single <tags> wrapping <item> elements.
  • Element names get sanitized automatically. Any character that isn't a letter, digit, underscore, period, or hyphen is replaced with _, and a name that doesn't start with a letter or underscore gets one added — a key like "2023 sales" becomes the tag _2023_sales.
  • null and {} both become self-closing tags. {"a":null,"b":{}} outputs <a/> and <b/> — there's no way to tell them apart in the XML itself.
  • Text is escaped for &, <, >, and ". A value like Tom & Jerry <say> "hi" comes out as Tom &amp; Jerry &lt;say&gt; &quot;hi&quot;, which keeps the document well-formed.
  • XML to JSON — convert the other direction, turning XML documents back into JSON.
  • JSON Formatter — validate, pretty-print, minify and sort keys before you convert.
  • JSON Validator — check your JSON and pinpoint syntax errors by line and column first.
  • CSV to JSON — if your source data is actually a spreadsheet, start here instead.

Frequently asked questions

Why doesn't the Root element option change my output?

If your top-level JSON is an object with exactly one key, that key becomes the root tag automatically and the Root element setting is ignored. It only applies to arrays, objects with two or more top-level keys, and bare values (a string, number, or boolean on its own).

What happens to arrays nested inside an object?

They don't get wrapped in the Array item element tag. Instead, each entry becomes a sibling tag with the same name as the key that held the array — so "tags":["ceramic","blue"] becomes two <tags> elements next to each other, not one <tags> containing <item> elements.

How does the tool handle invalid characters in key names?

Any character that isn't a letter, digit, underscore, period, or hyphen is replaced with an underscore, and if the result doesn't start with a letter or underscore, one is added in front — so XML tag names stay valid no matter what your JSON keys look like.

Can I get a bare XML fragment without the declaration?

Yes — turn off XML declaration and the output skips the <?xml version="1.0" encoding="UTF-8"?> line, which is useful when you're embedding the result inside a larger document.

Is my JSON sent to a server to convert it?

No. The conversion happens entirely in your browser. Signed-out visitors have nothing saved; if you're signed in, your input syncs privately to your account.

More guides

All guides →