Utility Coder
Back to Blog
converter

JSON To XML

Andy Pham
json to xml, json to xml, json to xml online

Ready to try JSON To XML?

Access the free online tool now - no registration required

Open Tool

JSON To XML - Complete Guide

JSON to XML conversion transforms JSON data into XML format for systems that require XML input, SOAP services, or XML-based configurations. This enables modern JSON applications to communicate with legacy systems and XML-based APIs.

How It Works

The converter maps JSON structures to XML elements: objects become parent elements with child elements for each property, arrays become repeated elements, and primitives become text content. A root element wraps the entire structure since XML requires a single root.

Technical Details

Conversion challenges: JSON has no attribute concept (use conventions like @attr), arrays need element names (use parent name or "item"), null handling (empty element, xsi:nil, or omit), and root element naming. Output can include XML declaration, namespaces, and formatting.

Common Use Cases

  1. SOAP Service Calls: Generate XML request bodies from JSON data.
    • Example: Convert form data to SOAP envelope
  2. Legacy System Integration: Send data to XML-only backend systems.
  3. Configuration Generation: Generate XML config files from JSON templates.
  4. Document Generation: Create XML documents (RSS, Atom, SVG) from JSON.

Code Examples

Node.js with xml2js

const xml2js = require('xml2js');

const json = {
  user: {
    $: { id: '123' },  // attributes
    name: 'John Doe',
    email: 'john@example.com'
  }
};

const builder = new xml2js.Builder({
  rootName: 'root',
  headless: false  // include XML declaration
});

const xml = builder.buildObject(json);

xml2js Builder converts objects to XML. $ is used for attributes by default.

Python with dicttoxml

from dicttoxml import dicttoxml
from xml.dom.minidom import parseString

data = {
    'user': {
        'name': 'John Doe',
        'email': 'john@example.com',
        'roles': ['admin', 'user']
    }
}

xml_bytes = dicttoxml(data, custom_root='root', attr_type=False)
# Pretty print
pretty = parseString(xml_bytes).toprettyxml()

dicttoxml handles nested structures and arrays. attr_type=False removes type attributes.

Tips & Best Practices

  • Define clear conventions for attributes vs elements
  • Always specify a meaningful root element name
  • Test output against target XML schema or XSD
  • Use pretty printing for human-readable config files
  • Handle null values consistently based on requirements

Common Mistakes to Avoid

  • Forgetting XML requires a single root element
  • Not handling arrays with proper element names
  • Producing invalid XML from special characters
  • Ignoring namespace requirements of target system

When to Use This Tool

Use JSON to XML when sending data to SOAP services, generating XML configurations, creating RSS/Atom feeds, or integrating with any system requiring XML format.

Frequently Asked Questions

How do I create XML attributes from JSON?

Use a convention like @attributeName in your JSON. Most converters recognize this: {"element": {"@id": "123", "#text": "content"}} becomes content.

What root element is used?

You typically specify the root element name. If JSON is an object with one key, that key often becomes the root. Arrays need an explicit root and item element names.

How are JSON arrays converted?

Arrays become repeated elements. {"items": [1,2,3]} might become 123. The inner element name varies by converter.

What about JSON null values?

Options: empty element (), xsi:nil attribute (), omit entirely, or use empty string. Choose based on the target XML schema requirements.

Get Started

Ready to try JSON To XML? Use the tool now - it's free, fast, and works right in your browser.


Last updated: June 27, 2026

Start using JSON To XML

Free, fast, and privacy-focused - try it now

Launch Tool