JSON to XML Tutorial: Convert JSON to XML with Examples

JSON and XML are both data interchange formats, but they express the same information differently. This guide walks through every conversion rule — from simple key-value pairs to nested arrays — with before-and-after examples you can paste directly into a converter.

Quick start: use the online tool

The fastest way to convert JSON to XML is to paste your JSON into the JSON to XML converter. The result includes a proper XML declaration and handles all the edge cases described below automatically.

How JSON objects map to XML elements

Each JSON key becomes an XML element tag. The value becomes the element's content. A wrapping root element is added because XML requires a single root node.

// JSON
{
  "name": "Alice",
  "age": 30,
  "active": true
}
<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name>Alice</name>
  <age>30</age>
  <active>true</active>
</root>

How JSON arrays map to XML

XML has no native array type. The standard convention is to repeat the same tag for each array item. When a JSON array is nested under a key, each element becomes a sibling element with that key's name.

// JSON
{
  "colors": ["red", "green", "blue"]
}
<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <colors>red</colors>
  <colors>green</colors>
  <colors>blue</colors>
</root>

Top-level arrays are wrapped in a root element with items named item:

// JSON
[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob" }
]
<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <item>
    <id>1</id>
    <name>Alice</name>
  </item>
  <item>
    <id>2</id>
    <name>Bob</name>
  </item>
</root>

How JSON null maps to XML

JSON null has no direct XML equivalent. The common convention is a self-closing tag, which signals the element is present but has no value.

// JSON
{ "middleName": null }
<!-- XML -->
<root>
  <middleName/>
</root>

Nested objects

Nested JSON objects map to nested XML elements:

// JSON
{
  "user": {
    "name": "Alice",
    "address": {
      "city": "New York",
      "zip": "10001"
    }
  }
}
<!-- XML -->
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <user>
    <name>Alice</name>
    <address>
      <city>New York</city>
      <zip>10001</zip>
    </address>
  </user>
</root>

Special characters in values

XML requires certain characters to be escaped inside element content. A converter handles this automatically, but it's useful to know the rules:

CharacterXML escape
&&amp;
<&lt;
>&gt;
"&quot;
'&apos;

Tag naming rules

XML tag names must start with a letter or underscore and contain only letters, digits, hyphens, underscores, and periods. JSON keys that violate these rules are sanitized automatically by most converters — for example, a key like "1stItem" becomes_1stItem.

JSON vs XML: when to use each

ScenarioBetter choice
REST APIsJSON
SOAP servicesXML
Config filesJSON or YAML
Document markupXML
JavaScript frontendsJSON
Enterprise integrationsXML

Convert JSON to XML online

Paste your JSON into the JSON to XML converter to get well-formed XML with an XML declaration and proper escaping. For the reverse direction, use the XML to JSON converter.