Skip to the content

Contact usFree tools · No signup · Files stay on your device

Developer Tools

JSON to XML: the complete guide to converting JSON data

JSON to XML: the complete guide to converting JSON data

JSON is the format most web APIs speak, but plenty of systems still want XML: payment gateways, government and banking integrations, older SOAP services and feed readers. Converting between them is not a matter of changing brackets for tags, because XML has attributes, a single root and strict naming rules that JSON does not. Our free JSON to XML converter handles those differences and tells you what it changed.

This guide explains how each part of your JSON turns into XML, what every option does, and how to avoid the surprises, such as a root element that appears from nowhere. It runs in your browser.

What JSON to XML does

It reads your JSON, and if it is valid it writes well-formed XML with an optional XML declaration and the indent you choose. Arrays become repeated elements, keys that start with @ become attributes, a #text key becomes element text, and names XML cannot use are repaired.

It does not infer a schema, it does not add namespaces, and it does not convert in a way that guesses types: every value is written as text, because XML has no numbers or booleans. If the JSON is invalid, you get the error and no output.

How to use JSON to XML, step by step

  1. Open JSON to XML.
  2. Paste JSON into the box labelled "Paste JSON", or press Open file (it accepts .json), or Sample.
  3. Under Options, set the Root element name and the Array item name.
  4. Choose Attribute keys (@name keys, _name keys or One $attrs object) and the Indent: 2 spaces, 4 spaces, Tabs or Minified.
  5. Keep Add XML declaration ticked if you want the <?xml version="1.0" encoding="UTF-8"?> line.
  6. Read the "XML output" panel. Under it you see Elements, Attributes and Output size, and a note for each thing that was renamed or changed.
  7. Press Copy, or Download .xml. The file takes the name of the file you opened, or data.xml.

A worked example

Take this order JSON:

{"shop":{"@id":"S1","order":[{"@no":1,"customer":"Rahim Uddin","delivery address":"Mirpur, Dhaka","1st payment":450.50,"paid":false,"note":null},{"@no":2,"customer":{"@vip":true,"#text":"Nusrat Jahan"},"delivery address":"Chattogram","1st payment":1200,"paid":true,"note":"Call first & confirm"}],"total":1650.5}}

With the default options the XML is:

<?xml version="1.0" encoding="UTF-8"?>
<shop id="S1">
  <order no="1">
    <customer>Rahim Uddin</customer>
    <delivery_address>Mirpur, Dhaka</delivery_address>
    <_1st_payment>450.5</_1st_payment>
    <paid>false</paid>
    <note/>
  </order>
  <order no="2">
    <customer vip="true">Nusrat Jahan</customer>
    <delivery_address>Chattogram</delivery_address>
    <_1st_payment>1200</_1st_payment>
    <paid>true</paid>
    <note>Call first &amp; confirm</note>
  </order>
  <total>1650.5</total>
</shop>

The stats show 14 elements and 4 attributes. A note lists the two renamed keys: "delivery address" became delivery_address, and "1st payment" became _1st_payment. The single top-level key shop is the root. The order array became two repeated order elements. @no and @vip are attributes, #text supplied the text of the second customer, null became an empty <note/>, and the & in the note was escaped as &amp;. Also note that 450.50 is written as 450.5, because the converter reads the number before writing it.

The rules

In the JSON In the XML
A key with a text, number or true/false value An element with that text
null An empty element, such as <note/>
An array under a key The element repeated once per item
An array with no key of its own Each member written as an item element
A key starting with @ An attribute on the parent element
A key called #text The text of the parent element
An empty array under a key Nothing is written

Every option explained

Root element name

XML has exactly one root. If your JSON has one key at the top level, and that value is not an array, that key becomes the root and this setting is not used. If it has several keys, is an array, or is a bare value, the whole thing is wrapped in this name. The default is root. Beware of the single-key array case: {"books":["a","b"]} has one key, but its value is an array, so you get <root><books>a</books><books>b</books></root>, not <books> as the root. Type your own name here, such as students or invoice. Names are limited to 80 characters and are repaired if invalid.

Array item name

Used for arrays that have no key to name them: a top-level array, or an array inside another array. For [1,2,{"a":3}] with the default item, you get a root holding three <item> elements. Set it to something meaningful, such as row or student. Arrays under a key do not use this setting; they repeat the key's own name.

Attribute keys: @name keys, _name keys or One $attrs object

This tells the converter how your JSON marks attributes.

  • @name keys: "@id": 7 becomes id="7". This is the default, and it matches the XML to JSON converter, so a round trip works.
  • _name keys: "_id": 7 becomes id="7".
  • One $attrs object: "$attrs": { "id": 1 } becomes id="1".

Be careful with _name keys. Any key that starts with an underscore and has at least one more character, such as MongoDB's _id, becomes an attribute. In our tests, {"_id":"64f","name":"Rahim"} with that setting gave <root id="64f"><name>Rahim</name></root>. With the default it gives <_id>64f</_id> as a normal element.

Indent

2 spaces, 4 spaces, Tabs or Minified. Minified writes everything on one line with no spaces between tags, which is what you want for sending in a request.

Add XML declaration

On by default. It adds <?xml version="1.0" encoding="UTF-8"?> at the top. Untick it when you are pasting the result inside another XML document, where a second declaration would be an error.

What it accepts and its limits

  • Input: any valid JSON text. A byte-order mark at the start is ignored.
  • Depth: more than 512 levels of nesting is refused with a message.
  • Large input: above 1 MB the page waits for you to press Convert.
  • Display: the panel shows the first 200,000 characters of a very large result; Copy and Download .xml give everything.
  • Numbers: read as JSON numbers first, so very long integers lose digits beyond what JavaScript can hold. Send long IDs as strings.
  • Attribute values that are objects or arrays are written as JSON text, and a note tells you how many.

Privacy

JSON to XML runs in your browser. Your JSON is not sent to our server, so API responses and configuration are safe to paste.

Common problems and how to fix them

"Invalid JSON at line 1, column 11: Unexpected end of input"

The JSON is incomplete or has a syntax error. The message gives the position and a reason, and Go to error selects it. Typical ones are a missing closing bracket, single quotes around a key ("Property names must use double quotes") and a trailing comma. The JSON Validator guide lists them all.

Two renamed keys became the same name

If first name and first_name both exist in one object you will get two elements with the same name. Rename one in the JSON first.

The root is not the element I wanted

You have several top-level keys, a top-level array, or a single key holding an array. Set the Root element name, or wrap your JSON in an object with one key.

The attributes appear as ordinary elements

The attribute style does not match your JSON. If your keys start with @, leave @name keys selected. If they start with _, choose _name keys.

The order of children is not what I expected

Children are written in the order the keys appear. When an element has both #text and children, the text is written first, on its own line.

Quick checklist

  • Check the root: one top-level key means that key is the root.
  • Use @ keys for attributes and #text for text.
  • Read the notes for renamed keys.
  • Untick the declaration if the XML goes inside another document.
  • Send long IDs as strings.

Frequently asked questions

Why did I get a <root> element I never wrote?
XML needs exactly one top-level element. If your JSON has one key at the top, that key becomes the root. If it has several keys, is an array, or is a single value, the tool wraps it in the root name you set, which is root by default.
How do I write an XML attribute in JSON?
Start the key with @, so {"book": {"@id": 7, "title": "Dune"}} gives <book id="7"><title>Dune</title></book>. A key called #text becomes the text of the element. You can switch to _name keys or a $attrs object.
Why was a key renamed?
XML names cannot contain spaces or most symbols, and cannot start with a digit. Spaces and symbols become underscores and a leading digit gets an underscore in front, so first name becomes first_name. A note under the output lists every renamed key.
What happens to an empty array?
An empty array under a key writes nothing, because there are no items to repeat. An empty array that is itself an item, or the whole JSON, is written as an empty element.
Why does my _id key become an attribute?
If you choose _name keys in Attribute keys, every key that starts with an underscore and has more characters after it is treated as an attribute. Keep the default @name keys when your JSON uses _id as a normal field.
Found this useful? Share it

Tools in this guide

Main tool

JSON to XML

Paste JSON and get well-formed XML with an optional declaration and your choice of indent.

Open tool

XML to JSON

Paste XML or open a file and get clean JSON.

Open tool

XML Formatter

Paste XML to check that it is well-formed and re-print it cleanly, or squeeze it down to one line.

Open tool

JSON Validator

Check whether your JSON is valid and get the exact line, column and reason for any error.

Open tool

Back to all guides & blog