JSON to XML Conversion: A Developer's Complete Guide
Modern APIs speak JSON, but legacy systems need XML. Converting between them requires handling arrays, data types, and structural differences properly.
Format Differences
JSON is lightweight and JavaScript-friendly:
JSON{ "name": "John Doe", "age": 30, "skills": ["JavaScript", "Python"], "active": true }
XML is verbose but self-documenting:
XML<?xml version="1.0" encoding="UTF-8"?> <user active="true"> <name>John Doe</name> <age>30</age> <skills>JavaScript</skills> <skills>Python</skills> </user>
Main Challenges
Arrays cause headaches. JSON uses ["item1", "item2"]
but XML uses repeated elements <item>item1</item><item>item2</item>
Converting back to JSON creates ambiguity is it an array or single value?
Data types get lost. JSON's 30 (number) becomes XML's "30" (string). Always validate types after conversion.
Root elements are required. JSON allows multiple top-level properties, but XML needs one root element. Choose meaningful names like <userProfile>
instead of <root>
Handling Common Issues
Array Conversion
JSON arrays become repeated XML elements:
JSON:
JSON{ "users": [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"} ] }
XML:
XML<root> <users> <user> <id>1</id> <name>Alice</name> </user> <user> <id>2</id> <name>Bob</name> </user> </users> </root>
Special Characters
XML reserves certain characters that need escaping:
<
becomes<
>
becomes>
&
becomes&
"
becomes"
'
becomes'
Invalid Element Names
JSON keys might not be valid XML element names:
"1st"
→"_1st"
(can't start with number)"first name"
→"first_name"
(no spaces)"user@domain"
→"user_domain"
(no special chars)
Best Practices
Use Meaningful Root Elements
Instead of generic <root>
, use descriptive names:
XML<!-- Good --> <customerOrder> <customerId>12345</customerId> <orderDate>2025-01-26</orderDate> </customerOrder> <!-- Avoid --> <root> <customerId>12345</customerId> <orderDate>2025-01-26</orderDate> </root>
Handle Null Values
Choose one approach for JSON null
values:
- Empty elements:
<middleName></middleName>
- Omit entirely: Skip the element
- Explicit null:
<middleName xsi:nil="true"/>
Preserve Data Types
Add type information when needed:
XML<user> <age type="number">30</age> <isActive type="boolean">true</isActive> </user>
Real-World Examples
API Integration
Convert JSON API responses for legacy XML systems:
JSON Response:
JSON{ "status": "success", "orders": [ {"id": 123, "total": 99.99}, {"id": 124, "total": 149.99} ] }
XML Output:
XML<orderResponse status="success"> <orders> <order id="123" total="99.99"/> <order id="124" total="149.99"/> </orders> </orderResponse>
Configuration Migration
Transform JSON config to XML:
JSON Config:
JSON{ "database": { "host": "localhost", "port": 5432, "ssl": true }, "features": ["auth", "logging"] }
XML Config:
XML<configuration> <database host="localhost" port="5432" ssl="true"/> <features> <feature>auth</feature> <feature>logging</feature> </features> </configuration>
Tools and Libraries
Online Tools
Use our JSON to XML converter for quick conversions. It processes data locally for privacy.
JavaScript/Node.js
JavaScriptimport { Builder } from 'xml2js'; const builder = new Builder({ rootName: 'root', xmldec: { version: '1.0', encoding: 'UTF-8' } }); const xml = builder.buildObject(jsonData);
Python
Pythonimport json import dicttoxml json_data = json.loads(json_string) xml_data = dicttoxml.dicttoxml(json_data, custom_root='root')
Java
Javaimport com.fasterxml.jackson.dataformat.xml.XmlMapper; XmlMapper xmlMapper = new XmlMapper(); String xml = xmlMapper.writeValueAsString(jsonObject);
Related Resources
Check out W3Schools XML Tutorial for XML basics and JSON.org for JSON specification.
For the reverse process, read our XML to JSON Conversion Best Practices. Our Essential Developer Tools 2025 covers more conversion utilities.
Converting JSON to XML is about reliable data transformation. Use our JSON to XML converter for quick conversions, and established libraries for production systems.