jsonxmlconvertersoaptutorial

JSON to XML Converter — How It Works and When to Use It

·8 min read·Converters

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).

json
{
  "user": {
    "name": "Ravi Mehta",
    "age": 28,
    "verified": true,
    "roles": ["admin", "editor"]
  }
}

Converts to:

xml
<?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:

CharacterEscaped formMeaning
&&amp;Ampersand — starts an entity reference
<&lt;Less-than — starts a tag
>&gt;Greater-than — ends a tag
"&quot;Double quote — inside attribute values
'&apos;Single quote — inside attribute values

JSONKit handles all escaping automatically. A JSON value like "AT&T" becomes <company>AT&amp;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:

json
{
  "product": {
    "@id": "101",
    "@category": "electronics",
    "name": "Laptop"
  }
}

Produces:

xml
<product id="101" category="electronics">
  <name>Laptop</name>
</product>

Converting JSON to XML in Node.js

javascript
// 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

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 caseFormatWhy
Modern REST APIJSONSimpler, universal browser/library support
SOAP web serviceXMLProtocol requires XML envelopes
RSS / Atom feedXMLFeed readers expect XML
Microsoft OfficeXML (OOXML)DOCX/XLSX are ZIP+XML internally
Kubernetes (old)YAML or JSONOfficial API accepts both
SAP / legacy ERPXML or EDILegacy 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.

Try JSON to XML

Generate well-formed XML from any JSON object.