JSON to XML Conversion: A Step-by-Step Tutorial
By The Smart Data Converter Team · 12 min read ·
JSON dominates modern APIs, but plenty of systems — SOAP services, enterprise middleware, legacy integrations — still require XML. Converting between them is mostly mechanical, with a few decisions around arrays, attributes, and special characters. This tutorial walks through all of them.
The basic mapping
A JSON object becomes an XML element; each property becomes a child element:
// JSON
{ "name": "Alice", "age": 30 }
<!-- XML -->
<person>
<name>Alice</name>
<age>30</age>
</person>
JSON has no single "root" name, while XML requires exactly one root element. So the first decision is always: what do I call the root? (Here, person.)
Representing arrays
JSON arrays have no direct XML equivalent, which is the part people most often get wrong. The standard approach is to repeat an element once per item:
// JSON
{ "tags": ["vip", "newsletter"] }
<!-- XML: repeat the element -->
<tags>vip</tags>
<tags>newsletter</tags>
An alternative wraps items in a container with singular child names, which many schemas prefer:
<tags>
<tag>vip</tag>
<tag>newsletter</tag>
</tags>
Pick one convention and apply it everywhere. The wrapper-with-singular-children style is usually friendlier to XSD validation.
Attributes vs. elements
Some JSON-to-XML conventions map keys prefixed with @ to attributes:
// JSON
{ "user": { "@id": "1", "name": "Alice" } }
<!-- XML -->
<user id="1">
<name>Alice</name>
</user>
This is a convention, not a rule — agree on it with whatever system consumes the XML.
Handling data types
JSON distinguishes strings, numbers, booleans, and null. XML content is just text, so types are lost unless a schema restores them:
| JSON | XML | Note |
|---|---|---|
| 30 | <age>30</age> | Number becomes text |
| true | <active>true</active> | Boolean becomes text |
| null | <note/> or omitted | Decide a null convention |
Escaping special characters
Just as in any XML, the characters &, <, and > (plus quotes inside attributes) must be escaped. A value like "Tom & Jerry" becomes Tom & Jerry. Forgetting this is the number-one cause of invalid output.
Method 1: Convert instantly online
Paste your JSON into Smart Data Converter to parse and inspect it, then export the structure you need — all in your browser, with nothing uploaded. It's the fastest way to get a correct base structure you can refine.
Method 2: Convert in Python
import json
from dicttoxml import dicttoxml # pip install dicttoxml
data = json.loads(json_string)
xml_bytes = dicttoxml(data, custom_root="person", attr_type=False)
print(xml_bytes.decode("utf-8"))
The attr_type=False flag stops the library from adding type="..." attributes you probably don't want.
Method 3: Convert in JavaScript
Libraries such as xmlbuilder2 give you precise control over namespaces and attributes:
import { create } from "xmlbuilder2";
const doc = create({ person: JSON.parse(jsonString) });
const xml = doc.end({ prettyPrint: true });
A note on namespaces
Enterprise XML often uses namespaces (xmlns) to avoid element-name collisions. JSON has no equivalent, so if the target schema requires namespaces you must add them during conversion — usually by configuring your XML builder with the right prefix and URI.
Validation before you send
Always confirm the output is at least well-formed, and validate against the recipient's XSD when one exists. A 30-second validation step prevents the most common integration failures.
Frequently asked questions
How are JSON arrays represented in XML?
By repeating an element per item, optionally inside a wrapper element with singular child names. Choose one style and use it consistently.
How do I add a root element?
Pick a meaningful name and wrap the whole document in it — XML allows exactly one root, while JSON has none.
Can I convert XML back to JSON?
Yes, though attributes and repeated elements require a mapping convention. The reverse direction is covered in our format comparison: JSON vs XML vs CSV.
JSON-to-XML comes down to three choices: the root name, how arrays are represented, and how attributes are mapped. Decide those once, escape special characters, validate against a schema, and your conversions will be predictable and correct.