Why Convert JSON to XML?
JSON has largely replaced XML for REST APIs, but XML remains the required format for SOAP web services, RSS/Atom feeds, Microsoft Office file formats (DOCX, XLSX), EDI integrations, and many legacy enterprise systems. Converting JSON to XML is essential when your modern API needs to talk to an older XML-based system.
How the Conversion Works
Each JSON key becomes an XML element tag. String, number, and boolean values become the element's text content. Nested objects become nested elements. Arrays become repeated child elements (with an item or configurable element name).
{
"user": {
"name": "Ravi Mehta",
"age": 28,
"verified": true,
"roles": ["admin", "editor"]
}
}Converts to:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<user>
<name>Ravi Mehta</name>
<age>28</age>
<verified>true</verified>
<roles>
<item>admin</item>
<item>editor</item>
</roles>
</user>
</root>XML Tag Naming Rules
XML tag names have strict rules that JSON keys do not:
- Must start with a letter or underscore, not a digit
- Cannot contain spaces
- Cannot contain <, >, &, or "
- Are case-sensitive (<Name> and <name> are different elements)
When a JSON key violates these rules, the converter replaces invalid characters with underscores. For example, "first name" becomes <first_name> and "2fa_enabled" becomes <_2fa_enabled>.
Configurable Root Tag
Every valid XML document must have exactly one root element. JSONKit wraps all output in a configurable root tag — the default is <root> but you can set any valid tag name in the converter settings. The output re-converts instantly when you change it.
For a SOAP envelope you would typically set the root tag to match the expected element name in the WSDL contract.
XML Special Characters — Automatic Escaping
XML reserves five characters that must be escaped inside text content:
| Character | Escaped form | Meaning |
|---|---|---|
| & | & | Ampersand — starts an entity reference |
| < | < | Less-than — starts a tag |
| > | > | Greater-than — ends a tag |
| " | " | Double quote — inside attribute values |
| ' | ' | Single quote — inside attribute values |
JSONKit handles all escaping automatically. A JSON value like "AT&T" becomes <company>AT&T</company> in the XML output.
Attributes vs Child Elements
JSON has no concept of XML attributes — it only has keys and values. When converting JSON to XML, all values become child elements (not attributes) by default. This is the simplest and most interoperable approach.
If the XML schema you are targeting requires attributes, you will need to post-process the generated XML. A common pattern used by libraries is to treat JSON keys starting with @ as attributes:
{
"product": {
"@id": "101",
"@category": "electronics",
"name": "Laptop"
}
}Produces:
<product id="101" category="electronics">
<name>Laptop</name>
</product>Converting JSON to XML in Node.js
// npm install fast-xml-parser
import { XMLBuilder } from "fast-xml-parser";
const data = {
user: {
name: "Ravi Mehta",
age: 28,
roles: { item: ["admin", "editor"] }
}
};
const builder = new XMLBuilder({
format: true,
indentBy: " ",
ignoreAttributes: false,
});
const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + builder.build({ root: data });
console.log(xml);Converting JSON to XML in Python
# pip install xmltodict
import json, xmltodict
data = {
"root": {
"user": {
"name": "Ravi Mehta",
"age": 28,
"roles": { "item": ["admin", "editor"] }
}
}
}
xml_output = xmltodict.unparse(data, pretty=True, indent=" ")
print(xml_output)SOAP vs REST Use Cases
| Use case | Format | Why |
|---|---|---|
| Modern REST API | JSON | Simpler, universal browser/library support |
| SOAP web service | XML | Protocol requires XML envelopes |
| RSS / Atom feed | XML | Feed readers expect XML |
| Microsoft Office | XML (OOXML) | DOCX/XLSX are ZIP+XML internally |
| Kubernetes (old) | YAML or JSON | Official API accepts both |
| SAP / legacy ERP | XML or EDI | Legacy system requirement |
Use JSONKit's JSON to XML converter to convert any JSON payload in your browser — no library install, no server upload, instant output with configurable root tag.