
JSON to XML Converter — When and How to Transform Data Formats
📷 Photo by Pixabay / PexelsJSON to XML Converter — When and How to Transform Data Formats
Learn when you need JSON to XML conversion, how the transformation works under the hood, and the edge cases that trip developers up.
If you work in software long enough, you will eventually encounter a system that only speaks XML. Maybe it's a SOAP API from a bank. Maybe it's an enterprise ERP integration. Maybe it's a government data feed that was built in 2003 and hasn't changed since. Whatever the source, you'll have data in JSON format — because that's what everything modern produces — and you'll need it in XML.
That's what the JSON to XML Converter is for. Paste your JSON, get clean XML out the other side. But understanding what's actually happening during the conversion, and where it can go wrong, will save you debugging time when the output doesn't look right.
Why JSON and XML Still Coexist
XML (eXtensible Markup Language) was introduced in 1998 as a standard for structured data that could be both human-readable and machine-readable. Through the late 1990s and 2000s, it became the lingua franca of enterprise software: SOAP web services, configuration files, document formats (Office XML, SVG, XHTML), and data interchange between large systems all used XML.
JSON (JavaScript Object Notation) emerged as a simpler alternative, formally specified in 2001 by Douglas Crockford. It was native to JavaScript, significantly less verbose than XML, and far easier to read and write by hand. As REST APIs became the norm and JavaScript took over the web, JSON became the default for API responses.
But here's the thing: the systems built on XML didn't disappear. Mainframes still run. SAP and Oracle ERP systems still use XML. SOAP web services are still alive in banking, insurance, healthcare, and government. XSLT, SVG, and RSS are XML-based and not going anywhere.
So you end up with a split world. New APIs speak JSON. Legacy systems speak XML. Integration between them requires conversion, often in both directions.
Real Scenarios Where You Need JSON to XML
SOAP API integrations: The classic case. SOAP (Simple Object Access Protocol) requires XML-formatted request bodies. If your application receives data from a REST API in JSON, you need to convert it to XML before sending it to a SOAP endpoint. This comes up constantly in financial services, healthcare (HL7), and government systems.
Enterprise ERP and CRM systems: SAP, Oracle, and older Salesforce integrations often expose XML-based APIs or expect XML-formatted data files. When you're pulling data from a modern source and pushing it into one of these systems, conversion is necessary.
Maven and Ant build configs: Java build tools use XML (pom.xml for Maven, build.xml for Ant). If you're generating or modifying these configs programmatically from JSON data, conversion is needed.
Spring XML configuration: Older Spring applications use XML application context files. Converting JSON-defined bean configurations to Spring XML format is an occasional need.
RSS and Atom feeds: If you're building a feed from a JSON API or database, you need to output XML that conforms to the RSS or Atom specification.
Government and regulatory data submissions: Tax authorities, statistical agencies, and regulatory bodies in many countries still require XML-formatted data submissions. XBRL (for financial reporting) and various EDI formats are XML-based.
Archival and document formats: SVG for vector graphics, DocBook for technical documentation, and various archival standards are XML-based.
How the Conversion Actually Works
The basic concept is straightforward: JSON's key-value pairs map to XML elements, JSON objects become parent elements with child elements, and values become text content.
Simple objects
A simple JSON object like this:
{
"name": "Alice",
"email": "alice@example.com",
"age": 30
}
Becomes XML like this:
<root>
<name>Alice</name>
<email>alice@example.com</email>
<age>30</age>
</root>
The JSON keys become XML element names. The values become text content inside those elements. A wrapper root element is added because XML requires exactly one root node.
Nested objects
Nesting works naturally. A JSON object nested inside another becomes a parent XML element with its own children:
{
"user": {
"name": "Bob",
"address": {
"city": "Portland",
"country": "US"
}
}
}
Becomes:
<root>
<user>
<name>Bob</name>
<address>
<city>Portland</city>
<country>US</country>
</address>
</user>
</root>
Data types
JSON has explicit data types (string, number, boolean, null, array, object). XML is text-only — everything is a string in XML. This means:
"active": truebecomes<active>true</active>— the boolean becomes the literal text "true""count": 42becomes<count>42</count>— the number becomes text "42""score": 3.14becomes<score>3.14</score>— same treatment
This is usually fine for conversion purposes, but systems consuming the XML need to know to parse these back to their intended types.
The Tricky Part: Arrays
Arrays are where JSON to XML conversion gets complicated, because XML doesn't have a native array concept. XML can have repeated sibling elements with the same name, which is the closest equivalent — but there's no single standard for how to map JSON arrays to XML.
The most common approach uses a generic wrapper:
{
"colors": ["red", "green", "blue"]
}
Could become:
<root>
<colors>
<item>red</item>
<item>green</item>
<item>blue</item>
</colors>
</root>
Another common approach uses the parent key name for each element:
<root>
<colors>red</colors>
<colors>green</colors>
<colors>blue</colors>
</root>
And another approach uses numbered suffixes or index attributes.
There's no universally "correct" answer — it depends on what the receiving system expects. If you're converting for a specific SOAP API or XML schema, you need to check what structure that schema requires and potentially post-process the converted XML to match.
Arrays of objects are handled similarly:
{
"users": [
{ "name": "Alice", "role": "admin" },
{ "name": "Bob", "role": "user" }
]
}
Becomes something like:
<root>
<users>
<item>
<name>Alice</name>
<role>admin</role>
</item>
<item>
<name>Bob</name>
<role>user</role>
</item>
</users>
</root>
Common Gotchas and Edge Cases
Invalid XML tag names
XML element names have strict rules: they must start with a letter or underscore, cannot contain spaces, and cannot start with "xml" (case-insensitive). JSON keys, by contrast, can be almost anything.
This creates problems when JSON keys are:
- Numbers or start with numbers:
"1stItem"is an invalid XML tag. Common workaround: prefix with an underscore (_1stItem) or a configurable prefix. - Contain spaces:
"first name"can't become<first name>. Spaces are usually replaced with underscores or hyphens. - Contain special characters:
"price$"or"value@index"— special characters need to be removed or replaced.
Good converters handle this automatically or warn you. If you're writing conversion code yourself, you need to sanitize keys before using them as element names.
JSON null values
null in JSON has no direct XML equivalent. Common conventions:
- Omit the element entirely
- Include an empty element:
<fieldName/> - Use an
xsi:nil="true"attribute (for XSD-aware systems)
Which approach to use depends on the consuming system's expectations.
The root element problem
JSON objects can have multiple top-level keys. XML requires exactly one root element. This means a JSON object like:
{
"firstName": "Alice",
"lastName": "Smith"
}
Needs a wrapper root element in XML:
<root>
<firstName>Alice</firstName>
<lastName>Smith</lastName>
</root>
Most converters add a generic <root> wrapper. If your target XML schema expects a specific root element name (like <Customer> or <Request>), you'll need to either configure the converter or rename it after conversion.
XML attributes vs child elements
XML supports a concept that JSON simply doesn't have: attributes on elements. An XML element like <user id="42" active="true">Alice</user> has both attributes and content. JSON has no equivalent — everything is a key-value pair.
For JSON to XML, this distinction is usually ignored and everything becomes child elements. But if you need to produce XML with attributes (which many XML schemas require), you'll need additional logic — either a converter that supports special notation (@id conventions are common) or manual post-processing.
JSON vs XML: Knowing When to Use Each
JSON is better when:
- Building REST APIs
- Storing configuration in files that developers will read and write
- Transmitting data between web services and browsers
- Working in JavaScript/TypeScript environments
- File size and parse speed matter
XML is better when:
- The target system requires it (SOAP, many enterprise APIs)
- You need document structure (mixed content: text with embedded formatting tags)
- You need to attach metadata to individual fields (via attributes)
- Working with namespaces for combining data from multiple schemas
- Generating SVG, RSS, XHTML, or other XML-based standards
- Long-term archival where self-description matters
In most new development, JSON wins on simplicity. But the legacy XML world is enormous and not going away, which is why conversion tools matter.
Using the Converter
The JSON to XML Converter handles the common cases: simple objects, nested structures, arrays, and basic data types. Paste your JSON, and it produces formatted XML output you can copy or download.
For production integrations, always validate the output XML against the target schema (XSD) if one is available. The conversion handles the structure, but the element names, attribute requirements, and namespace declarations needed by a specific system may need additional adjustments.
For exploratory or one-off conversions — inspecting a payload, understanding a data structure, or quickly producing XML for a test — the converter handles it in seconds without needing to write any code or install anything.