Home › JSON to XML Converter

JSON to XML Converter Online

Convert JSON to well-formed XML instantly. Free online converter — handles nested objects, arrays, and all JSON data types. No signup required.

JSON to XML Conversion Example

JSON Input:

{
  "user": {
    "id": 101,
    "name": "Alice",
    "roles": [
      "admin",
      "editor"
    ],
    "active": true
  }
}

XML Output:

<?xml version="1.0"?>
<user>
  <id>101</id>
  <name>Alice</name>
  <roles>admin</roles>
  <roles>editor</roles>
  <active>true</active>
</user>

JSON to XML Mapping Rules

JSON XML Equivalent
Object {"key":"value"}<key>value</key>
Nested objectNested child elements
Array [1, 2, 3]Repeated elements with same tag name
String, Number, BooleanText content (all types become text)
null<key xsi:nil="true"/>

Convert JSON to XML in JavaScript

// Simple JSON to XML converter
function jsonToXml(obj, rootTag = 'root', indent = '') {
  if (Array.isArray(obj)) {
    return obj.map(item => `${indent}<item>${jsonToXml(item, 'item', indent + '  ')}</item>`).join('\n');
  }
  if (typeof obj === 'object' && obj !== null) {
    const children = Object.entries(obj)
      .map(([key, val]) => `${indent}  <${key}>${jsonToXml(val, key, indent + '  ')}</${key}>`)
      .join('\n');
    return `\n${children}\n${indent}`;
  }
  return String(obj);
}

const json = { name: 'Alice', age: 30 };
const xml = `<?xml version="1.0"?>\n<root>${jsonToXml(json)}</root>`;

// For production: use xml2js library
// npm install xml2js
import { Builder } from 'xml2js';
const builder = new Builder();
const xml = builder.buildObject(json);

Convert JSON to XML in Python

import json
import dicttoxml  # pip install dicttoxml

with open('data.json') as f:
    data = json.load(f)

xml_bytes = dicttoxml.dicttoxml(data, custom_root='root', attr_type=False)
print(xml_bytes.decode())

# Or using xmltodict:
import xmltodict
# xml → json: json.dumps(xmltodict.parse(xml_string))
# json → xml: xmltodict.unparse(json_dict, pretty=True)

When to Convert JSON to XML

Frequently Asked Questions

Can I convert XML back to JSON?

Yes. JSON Web Tools includes an XML to JSON converter. Paste your XML and get JSON output instantly — the tool handles attributes, namespaces, and mixed content.

What happens to JSON arrays in XML?

JSON arrays become repeated XML elements with the same tag name. For example, ["a","b","c"] under key items becomes <items>a</items><items>b</items><items>c</items>. XML has no native array concept, so this is the standard approach.

Are JSON numbers preserved in XML?

XML has no type system — all values are text. The number 42 becomes the string "42" in XML. When converting back from XML to JSON, you need to apply type coercion to restore numbers and booleans from their string representations.

Convert JSON to XML now

Free, instant, private — no signup required.

JSON to XML Conversion Examples

JSON and XML represent the same data in different formats. XML is still required by many SOAP APIs, enterprise systems, and document standards like XSLT, SVG, and RSS.

JSON Input → XML Output

// JSON Input
{
  "user": {
    "id": 1,
    "name": "Alice",
    "roles": ["admin", "user"]
  }
}

<!-- XML Output -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <user>
    <id>1</id>
    <name>Alice</name>
    <roles>
      <item>admin</item>
      <item>user</item>
    </roles>
  </user>
</root>

JSON vs XML Structure Comparison

JSONXML EquivalentExample
Object {}<element>...</element>Nested XML elements
Array []Repeated child elementsOr wrapped in a container
stringText node<name>Alice</name>
numberText node<id>1</id>
booleanText node<active>true</active>
nullEmpty or xsi:nil<val xsi:nil="true"/>
keyElement nameAttribute or child element

Convert in Python

# Using xmltodict library
import json, xmltodict

with open("data.json") as f:
    data = json.load(f)

xml_str = xmltodict.unparse({"root": data}, pretty=True)
print(xml_str)

JSON to XML Conversion Rules

Understanding how JSON types map to XML elements helps you predict the output and handle edge cases correctly.

// Input JSON
{
  "order": {
    "id": "ORD-123",
    "customer": "Alice",
    "items": [
      {"name": "Widget", "qty": 2, "price": 9.99},
      {"name": "Gadget", "qty": 1, "price": 24.99}
    ],
    "total": 44.97
  }
}
<!-- Output XML -->
<?xml version="1.0" encoding="UTF-8"?>
<order>
  <id>ORD-123</id>
  <customer>Alice</customer>
  <items>
    <item>
      <name>Widget</name>
      <qty>2</qty>
      <price>9.99</price>
    </item>
    <item>
      <name>Gadget</name>
      <qty>1</qty>
      <price>24.99</price>
    </item>
  </items>
  <total>44.97</total>
</order>

JSON to XML in Code

// Node.js with js2xmlparser
const js2xmlparser = require('js2xmlparser');
const result = js2xmlparser.parse('order', jsonData);
console.log(result);
# Python with dicttoxml
import json, dicttoxml
data = json.loads(json_string)
xml_bytes = dicttoxml.dicttoxml(data, custom_root='root', attr_type=False)
xml_str = xml_bytes.decode('utf-8')

When to Use XML vs JSON

Use Case XML JSON
SOAP web services✓ Required✗ Not supported
REST APIs✓ Possible✓ Preferred
RSS/Atom feeds✓ Standard✗ Not standard
HTML documents✓ Related (XHTML)
Android resources✓ Standard
Configuration files✓ Common✓ Common
Database storage✗ Rarely✓ Native JSON columns
JavaScript apps✓ Possible✓ Native

Also useful: JSON to CSV | JSON to YAML | JSON vs XML Guide | JSON Formatter