โ† Back to Blog

JSON to XML Conversion Guide

2026-04-06 ยท 5 min read

Why Convert JSON to XML?

Although JSON dominates modern Web APIs, XML remains irreplaceable in some contexts: SOAP web services (still widely used by many enterprises and banking systems); some government data interfaces and industry standards (HL7 medical data exchange, FinXML financial data); XML configuration files (Maven pom.xml, Spring configuration); data processing pipelines requiring XSLT transformation.

When you need to integrate modern JSON API data with legacy XML systems โ€” or vice versa โ€” conversion between formats is necessary. Understanding the correct conversion rules and tools prevents data loss and structural errors.

Basic Conversion Rules

Basic rules for JSON object to XML conversion: Each key in a JSON object becomes an XML element tag name; the corresponding value becomes the element's text content (for primitive types) or child elements (for objects); each item in an array typically repeats the parent tag name or uses a dedicated wrapper element.

// JSON
{
  "user": {
    "name": "Alice",
    "age": 30,
    "tags": ["admin", "user"]
  }
}

<!-- XML ็ป“ๆžœ / XML Result -->


    Alice
    30
    admin
    user


The Challenge of Array Conversion

JSON array to XML conversion is the most tricky part because XML has no direct equivalent to arrays. Three common approaches: the repeating element method (as in the example above โ€” each array item repeats the same tag name); the wrapper element method (a parent element wraps all array items: <tags><item>admin</item><item>user</item></tags>); the attribute method (storing array content as an XML attribute value, suitable for simple string arrays).

Each approach has applicable scenarios โ€” choose based on the XML format the target system expects. When integrating with third-party XML systems, always confirm the expected XML structure first before selecting a conversion strategy.

JSON to XML in Programming Languages

# Python
import json, xml.etree.ElementTree as ET
from dicttoxml import dicttoxml

data = {"user": {"name": "Alice", "age": 30}}
xml_bytes = dicttoxml(data, custom_root='root')

// JavaScript (Node.js - xml2js library)
const xml2js = require('xml2js');
const builder = new xml2js.Builder();
const xml = builder.buildObject(jsonData);

// Java (Jackson)
XmlMapper xmlMapper = new XmlMapper();
String xml = xmlMapper.writeValueAsString(jsonObject);

Handling XML-Specific Features in Conversion

XML has features that JSON lacks, requiring special handling in conversion: XML Attributes: JSON has no attribute concept; deciding whether to convert JSON keys to XML attributes or child elements requires human decision. A common convention is keys prefixed with @ become attributes (e.g., @id โ†’ id="..."); XML Namespaces: Complex XML systems frequently use namespaces that cannot be automatically inferred from pure JSON conversion โ€” additional configuration is needed; CDATA sections: Content with special characters needs CDATA wrapping; conversion tools usually handle this automatically.

XML Back to JSON: Reverse Conversion

Converting from XML back to JSON is typically more complex because XML's attributes, namespaces, text nodes, and other concepts don't have direct JSON equivalents โ€” decisions must be made about how to handle each. Common conversion strategies: attributes map to keys prefixed with @ or $; text content maps to #text or a similar special key; namespace prefixes are kept or removed.

Different conversion libraries handle these details differently. It's recommended to explicitly select one conversion library at the start of a project and use it consistently, avoiding data parsing errors from inconsistent conversion strategies.

Tips for Using Online Conversion Tools

When using online JSON-to-XML tools: first confirm JSON syntax is correct with a JSON formatter before converting; check whether the tool's array handling matches your requirements; validate the resulting XML with an XML validator to confirm it's well-formed; for JSON containing sensitive data, prioritize locally-processing tools or offline methods.

For batch conversion scenarios (converting many JSON files to XML), use command-line tools or write scripts rather than relying on online tools to process one at a time โ€” more efficient and reliable.

Try the free tool now

Use Free Tool โ†’