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 & 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.
- 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.
- Set the Root element name. The default is
root. - Choose Indentation (2 spaces, 4 spaces or Tab).
- 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. - 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. - Copy the XML output or Download it as an
.xmlfile.
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
dicttoxmlandxmltodictpackages convert between dictionaries and XML;xmltodict.unparseuses a similar@and#textconvention. - JavaScript: fast-xml-parser's
XMLBuilderis 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
- 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.
- Round-trip it. Converting back with XML to JSON shows whether anything was lost or renamed.
- Validate against the schema if one exists, using
xmllint --schemaor your IDE. Element order matters to many schemas, so order keys in the JSON the way the schema lists the elements. - 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.