How to Convert XML to JSON Online

Legacy enterprise APIs and SOAP web services speak XML. Modern REST APIs speak JSON. If you are building an integration between the two worlds — consuming an XML feed, transforming a SOAP response, or migrating data from an older system — converting XML to JSON is a task you will run into repeatedly.

Why the conversion is not trivial

JSON and XML represent structure differently. JSON has exactly two container types: objects (key-value maps) and arrays. XML has elements, attributes, text content, CDATA, and namespaces. There is no single official standard for mapping between them, which is why different tools produce different output.

The most widely used convention — and the one this converter follows — maps:

Example: simple element to JSON

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <name>Jane Smith</name>
  <age>28</age>
  <active>true</active>
</user>

Output JSON:

{
  "user": {
    "name": "Jane Smith",
    "age": "28",
    "active": "true"
  }
}

Note: XML has no type system — all values are text. Numbers and booleans remain strings in the JSON output. If you need typed values, you must cast them after conversion.

Example: XML attributes

XML attributes appear in an @attributes key inside the element's JSON object:

Input XML:

<product id="42" category="tools">
  <name>JSON Formatter</name>
  <price>0</price>
</product>

Output JSON:

{
  "product": {
    "@attributes": {
      "id": "42",
      "category": "tools"
    },
    "name": "JSON Formatter",
    "price": "0"
  }
}

Example: repeated elements become arrays

When multiple sibling elements share the same tag name, they are automatically grouped into a JSON array:

Input XML:

<library>
  <book>Clean Code</book>
  <book>The Pragmatic Programmer</book>
  <book>Refactoring</book>
</library>

Output JSON:

{
  "library": {
    "book": [
      "Clean Code",
      "The Pragmatic Programmer",
      "Refactoring"
    ]
  }
}

Example: nested XML structure

Input XML:

<order id="ORD-001">
  <customer>
    <name>Alice</name>
    <email>alice@example.com</email>
  </customer>
  <items>
    <item sku="A1">Widget</item>
    <item sku="B2">Gadget</item>
  </items>
</order>

Output JSON:

{
  "order": {
    "@attributes": { "id": "ORD-001" },
    "customer": {
      "name": "Alice",
      "email": "alice@example.com"
    },
    "items": {
      "item": [
        { "@attributes": { "sku": "A1" }, "#text": "Widget" },
        { "@attributes": { "sku": "B2" }, "#text": "Gadget" }
      ]
    }
  }
}

When an element has both attributes and text content, the text goes in a #text key alongside @attributes.

Common use cases for XML to JSON conversion

Convert XML to JSON instantly

Paste any XML document and get clean JSON output. Handles attributes, nested elements, repeated tags, and CDATA automatically. Free, private, no signup required.

Open XML to JSON Converter

Frequently Asked Questions

Why do all values come out as strings?

XML has no type system — every value is text inside an element. The converter preserves what is in the document. If you need "age": 28 instead of "age": "28", you need to post-process the JSON and cast the fields you know to be numeric.

How are namespaces handled?

Namespace prefixes (e.g., ns:element) are kept as-is in the JSON key name. Namespace declarations (xmlns:ns="...") appear as attributes.

Can I convert back from JSON to XML?

Yes — use the JSON to XML Converter. Note that the round-trip is not perfectly lossless if the original XML had attributes or mixed text/element content, since @attributes and #text keys become conventional XML structure.