How JSON Maps to XML - Root Elements, Arrays and Attributes

Learn how JSON objects, arrays, nulls and invalid key names become XML elements and attributes, and how to get the exact XML shape a legacy system expects.

Updated September 29, 2026 5 min read

Use the free JSON to XML ConverterBuild well-formed XML from JSON with a custom root

Modern services exchange JSON, but plenty of systems still expect XML: SOAP endpoints, e-invoicing and banking formats, marketplace product feeds, enterprise integration platforms and older desktop software. When you need to send data from a JSON world into one of these, a mechanical conversion is rarely enough. The receiver expects a specific root element, specific element names, and sometimes values as attributes rather than child elements.

This guide explains the rules for mapping JSON onto XML, where the two formats genuinely differ, how to control the shape of the output, and how to check the result before sending it.

Where JSON and XML differ

Both formats describe trees of data, but they are not built from the same pieces.

JSON XML
Top level can be an object, array or single value Exactly one root element required
Object keys can be any string Element names have strict rules
Arrays are a distinct type No array type; repeated elements instead
Types: string, number, boolean, null Everything is text
No attributes Elements can have attributes and text
Key order is not significant Element order often matters to a schema

Every converter has to make choices to bridge these gaps. Knowing the choices lets you shape your JSON so that the XML comes out right.

The mapping rules

Objects become nested elements

Each key becomes an element, and nested objects become nested elements:

{ "customer": { "name": "Dana Whitfield", "city": "Leeds" } }
<customer>
  <name>Dana Whitfield</name>
  <city>Leeds</city>
</customer>

Arrays become repeated elements

XML has no list syntax. An array is written as the same element repeated once per item:

{ "tags": ["vip", "net30"] }
<tags>vip</tags>
<tags>net30</tags>

Many XML schemas instead expect a wrapper with singular children: <tags><tag>vip</tag><tag>net30</tag></tags>. To get that shape, structure the JSON the same way: "tags": { "tag": ["vip", "net30"] }.

Values become text

Numbers and booleans are written as text (<qty>2</qty>, <paid>true</paid>). Special characters are escaped automatically, so Ring bell & wait becomes Ring bell &amp; wait. An empty string or null becomes a self-closing element such as <note/>. If the receiving system distinguishes between an empty element and a missing one, remove the key from the JSON rather than setting it to null.

Invalid names are repaired

XML element names cannot contain spaces, cannot start with a digit, and must not start with xml. JSON keys can be anything. A converter has to repair such keys, for example first name becomes first_name and 2fa becomes _2fa. If you see names you did not expect, rename the keys in the JSON before converting.

Attributes and text content

To produce attributes, you need a convention that marks some keys as attributes. Our converter uses the widely used @_ prefix, and #text for an element's own text:

{
  "invoice": {
    "@_id": "INV-1001",
    "@_currency": "EUR",
    "customer": "Acme Ltd",
    "total": { "@_tax": "included", "#text": 249.5 }
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<invoice id="INV-1001" currency="EUR">
  <customer>Acme Ltd</customer>
  <total tax="included">249.5</total>
</invoice>

The same convention is used by the popular fast-xml-parser library when reading XML, so JSON produced from XML in that style converts back cleanly.

Convert JSON to XML with our tool

The JSON to XML Converter applies these rules in your browser and updates the output as you type.

  1. Open the JSON to XML Converter. Paste JSON into JSON input, or click Open file. Load sample shows an order with attributes and a list of items.
  2. Set the Root element name. The default is root.
  3. Choose Indentation (2 spaces, 4 spaces or Tab).
  4. Keep XML declaration on to start the output with <?xml version="1.0" encoding="UTF-8"?>, or turn it off when the XML will be embedded in another document.
  5. Decide on Always wrap in root. If your JSON already has a single top-level key, such as invoice, that key is used as the root element and the root name is ignored. Turn this switch on to wrap it in your chosen root anyway.
  6. Copy the XML output or Download it as an .xml file.

When the JSON has several top-level keys, they are wrapped in the root element automatically. When the top level is an array, each entry becomes an <item> element inside the root. If the JSON has a syntax error, the output pane shows the line and column instead.

Other ways to do it

  • Python: the third-party dicttoxml and xmltodict packages convert between dictionaries and XML; xmltodict.unparse uses a similar @ and #text convention.
  • JavaScript: fast-xml-parser's XMLBuilder is the library our tool uses and is a good choice in Node.js projects.
  • Templates: when the target format is strict and complex (an e-invoice standard, for example), generating XML from a template or with a schema-aware library is more reliable than generic conversion, because it can enforce element order and required fields.

Generic conversion is ideal for quick integrations, test payloads and simple feeds. For regulated formats, treat its output as a starting point.

Checking the result

  1. Check well-formedness and readability. Paste the output into the XML Formatter. It confirms the document parses and lets you switch to a minified version if the receiver needs one.
  2. Round-trip it. Converting back with XML to JSON shows whether anything was lost or renamed.
  3. Validate against the schema if one exists, using xmllint --schema or your IDE. Element order matters to many schemas, so order keys in the JSON the way the schema lists the elements.
  4. Compare with a known-good sample. If the receiving system provides an example file, compare element names, nesting and attributes line by line.

If the source data is itself messy, tidy it first with the JSON Formatter so you can see the structure you are converting.

Frequently asked questions

Why does XML need a root element?

The XML specification requires every document to have exactly one top-level element containing everything else. JSON has no such rule, so a converter wraps the data in a root when needed.

How do I make a JSON value an XML attribute?

Prefix the key with @_, for example "@_id": "INV-1001". Use #text for the element's own text when it also has attributes.

How are JSON arrays converted?

Each item becomes a repeated element with the array's key as its name. For a wrapper element with singular children, nest the array under a singular key.

What happens to null values?

They become empty, self-closing elements such as <note/>. Remove the key entirely if the element should not appear.

Is JSON to XML conversion lossless?

Not completely. XML has no types, so numbers and booleans become text, and a single-item array looks the same as a single value. Keep the original JSON if you need to restore the exact structure.

Try it now: JSON to XML Converter

Loading tool…