XML to JSON: How Conversion Works with Examples

XML to JSON conversion changes XML data into a JSON structure. It is useful when older XML systems need to work with modern APIs, JavaScript applications or tools that prefer JSON.

Last updated: May 2026 Reading time: 7 minutes Reviewed for accuracy
Quick answer:
  • XML elements usually become JSON properties.
  • Nested XML becomes nested JSON objects.
  • Repeated XML elements usually become JSON arrays.
  • XML attributes often need special handling.

What is XML to JSON conversion?

XML to JSON conversion is the process of transforming data from XML markup into JSON key-value structures.

The goal is usually to make XML data easier to use in JavaScript, APIs or systems that expect JSON.

Simple idea:

XML is like a labelled filing cabinet. JSON is like a neatly packed backpack. Both can carry the same information, but they arrange it differently.

Simple XML to JSON example

Here is a simple XML example:

<employee>
  <name>Jane Smith</name>
  <role>Chief Cheese Tester</role>
</employee>

Converted to JSON, it might look like this:

{
  "employee": {
    "name": "Jane Smith",
    "role": "Chief Cheese Tester"
  }
}

How XML elements convert to JSON

XML elements usually become JSON properties.

XML JSON result
<name>Jane</name> "name": "Jane"
<role>Chief Cheese Tester</role> "role": "Chief Cheese Tester"

Nested XML to JSON

Nested XML structures become nested JSON objects.

<company>
  <employee>
    <name>Jane Smith</name>
    <role>Bridge Snack Coordinator</role>
  </employee>
</company>
{
  "company": {
    "employee": {
      "name": "Jane Smith",
      "role": "Bridge Snack Coordinator"
    }
  }
}

Repeated XML elements become arrays

When the same XML element appears more than once, it is usually converted into a JSON array.

<team>
  <member>Jane</member>
  <member>Mia</member>
  <member>Barry</member>
</team>
{
  "team": {
    "member": [
      "Jane",
      "Mia",
      "Barry"
    ]
  }
}

How XML attributes convert to JSON

XML attributes need special handling because JSON does not have a separate attribute concept.

<employee id="101">
  <name>Jane Smith</name>
</employee>

A common JSON mapping uses an @ prefix for attributes:

{
  "employee": {
    "@id": "101",
    "name": "Jane Smith"
  }
}

What about XML namespaces?

XML namespaces can make conversion more complex because JSON does not have built-in XML namespace support.

Some converters keep the prefix in the JSON property name, while others remove or map namespace data separately.

<book:title xmlns:book="https://cheesebridge.com/ns/book">
  CheeseBridge Adventures
</book:title>

Why XML to JSON conversion is not always perfect

XML and JSON are different formats, so conversion is not always one-to-one.

XML feature Why it can be tricky in JSON
Attributes JSON has properties, but no separate attribute system.
Namespaces JSON does not have built-in namespace handling.
Repeated elements The converter must decide when to create arrays.
Mixed content XML can mix text and elements in ways JSON does not naturally represent.
Order Some XML documents rely on order more heavily than JSON structures do.

When should you convert XML to JSON?

  • When an API returns XML but your application prefers JSON.
  • When moving old integrations into newer JavaScript systems.
  • When preparing XML data for frontend applications.
  • When making XML easier to inspect or transform.
  • When connecting enterprise systems to modern tools.

Convert XML to JSON online

Use CheeseBridge to convert XML into JSON directly in your browser, then format or inspect the result with JSON tools.

Open XML to JSON Converter Open XML Formatter Open JSON Formatter Open JSON Viewer

Trusted references

For official and technical references, see:

Frequently asked questions

Can XML be converted to JSON?

Yes. XML can often be converted to JSON, but attributes, namespaces, repeated elements and mixed content may need special handling.

What happens to XML attributes in JSON?

XML attributes are usually converted into JSON properties. Some converters use a prefix such as @ to show that the value came from an attribute.

Is JSON better than XML?

JSON is often better for modern web APIs because it is shorter and easier to use with JavaScript. XML is still useful when attributes, namespaces, schemas or document-style structures are needed.