JSON to XML Tutorial: Convert JSON to XML with Examples
Last updated:
Converting JSON to XML requires 4 structural decisions before writing a single line of output: root element name, array encoding strategy, null representation, and invalid key sanitization. JSON arrays have no native XML equivalent — the standard convention repeats the same element tag for each item, so a 3-item array produces 3 sibling elements. JSON null maps to a self-closing tag or xsi:nil="true", and numeric keys like "1" must be prefixed (e.g. _1) because XML tag names cannot start with a digit. This guide covers all 4 mapping rules, 5 escape characters required in XML values, attribute vs element decisions, and code examples in JavaScript (xml-js) and Python (dicttoxml).
Convert JSON to XML online
Paste any JSON to get a properly formatted XML document instantly — includes XML declaration and handles nested structures automatically.
Open JSON to XML ConverterHow JSON objects map to XML elements
Each JSON key becomes an XML element tag. The value becomes the element's content. A wrapping root element is added because XML requires a single root node.
// JSON
{
"name": "Alice",
"age": 30,
"active": true
}<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>Alice</name>
<age>30</age>
<active>true</active>
</root>How JSON arrays map to XML
XML has no native array type. The standard convention is to repeat the same tag for each array item. When a JSON array is nested under a key, each element becomes a sibling element with that key's name.
// JSON
{
"colors": ["red", "green", "blue"]
}<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
<colors>red</colors>
<colors>green</colors>
<colors>blue</colors>
</root>Top-level arrays are wrapped in a root element with items named item:
// JSON
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<id>1</id>
<name>Alice</name>
</item>
<item>
<id>2</id>
<name>Bob</name>
</item>
</root>How JSON null maps to XML
JSON null has no direct XML equivalent. The common convention is a self-closing tag, which signals the element is present but has no value.
// JSON
{ "middleName": null }<!-- XML -->
<root>
<middleName/>
</root>Nested objects
Nested JSON objects map to nested XML elements:
// JSON
{
"user": {
"name": "Alice",
"address": {
"city": "New York",
"zip": "10001"
}
}
}<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
<user>
<name>Alice</name>
<address>
<city>New York</city>
<zip>10001</zip>
</address>
</user>
</root>Special characters in values
XML requires certain characters to be escaped inside element content. A converter handles this automatically, but it's useful to know the rules:
| Character | XML escape |
|---|---|
& | & |
< | < |
> | > |
" | " |
' | ' |
Tag naming rules
XML tag names must start with a letter or underscore and contain only letters, digits, hyphens, underscores, and periods. JSON keys that violate these rules are sanitized automatically by most converters — for example, a key like "1stItem" becomes_1stItem.
JSON vs XML: when to use each
| Scenario | Better choice |
|---|---|
| REST APIs | JSON |
| SOAP services | XML |
| Config files | JSON or YAML |
| Document markup | XML |
| JavaScript frontends | JSON |
| Enterprise integrations | XML |
Convert JSON to XML online
Paste your JSON into the JSON to XML converter to get well-formed XML with an XML declaration and proper escaping. For the reverse direction, use the XML to JSON converter.
Frequently asked questions
How are JSON arrays represented in XML?
XML has no native array type. The convention is to repeat the same tag for each array item — a "colors" array with ["red","green"] becomes two sibling <colors> elements. Top-level arrays wrap each item in <item> inside a <root> element.
What is the XML equivalent of JSON null?
The most common convention is a self-closing empty element: <middleName/>. Some XML schemas use xsi:nil="true" to explicitly signal null. The right approach depends on the target XML schema — check whether it expects empty elements or omits null fields entirely.
How are attributes vs elements chosen when converting JSON to XML?
Generic converters map all JSON keys to child elements, not attributes, because JSON has no attribute concept. To produce attributes, use a converter that supports a prefix convention (like "@" for attribute keys) or write a custom transformation for your target XML schema.
Can I convert JSON to valid XML automatically?
Yes. Automatic conversion produces well-formed XML (single root, all tags closed, special characters escaped). It may not be schema-valid for a specific XSD — that may require post-processing to add namespaces, rename elements, or reorder structure.
What XML root element name is used?
Most converters use "root" by default since JSON objects have no inherent root name. For specific APIs or standards, set the root element name explicitly using the converter's rootName option to match the expected XML structure.
How does XML handle JSON numeric keys?
XML tag names cannot start with a digit. Most converters prefix numeric JSON keys with an underscore: a key "1" becomes <_1>. Verify the converter behavior and design JSON keys to start with a letter if XML output is planned.
Recommended reading
- Designing Data-Intensive Applications (2nd Edition) — Martin Kleppmann & Chris RiccominiThe modern classic on data systems — encoding formats, schemas, replication, and stream processing.
- API Design Patterns — JJ GeewaxBattle-tested patterns for designing consistent, scalable JSON APIs — from a Google API architect.
As an Amazon Associate, Jsonic earns from qualifying purchases.