XML to JSON

Convert XML to JSON instantly in your browser.

Local Your input is processed locally in your browser. Found a problem? Report an issue

What XML to JSON does

XML to JSON reads XML and writes it as JSON. The input is parsed first, so a syntax error is reported with its position instead of producing broken output.

XML to JSON examples

The built-in sample

Input

<users><user id="1"><name>Ada</name></user><user id="2"><name>Lin</name></user></users>

Output

{
  "users": {
    "user": [
      {
        "@id": "1",
        "name": "Ada"
      },
      {
        "@id": "2",
        "name": "Lin"
      }
    ]
  }
}

This is the input that the Load sample button fills in.

Attributes, repeated elements, CDATA and empty elements

Input

<order id="7"><item sku="A1">Pen</item><item sku="B2">Ink &amp; paper</item><note><![CDATA[1 < 2]]></note><paid/></order>

Output

{
  "order": {
    "@id": "7",
    "item": [
      {
        "@sku": "A1",
        "#text": "Pen"
      },
      {
        "@sku": "B2",
        "#text": "Ink & paper"
      }
    ],
    "note": "1 < 2",
    "paid": ""
  }
}

The two item elements become an array of objects with @sku and #text, the CDATA section becomes plain text, and the empty paid element becomes an empty string.

Each output was produced by running this tool on the input above it.

How it works

The converter reads your XML with the browser's standards-based XML parser. It reports well-formedness errors, which are the rules in the W3C XML 1.0 Recommendation: one root element, matching tags and escaped special characters. It then turns the document tree into JSON as defined by RFC 8259. The root element name becomes the single top-level key.

Data mapping rules

XMLJSON outputNote
<a id="1"/>{"a":{"@id":"1"}}Attributes get an @ prefix
<a>x</a>{"a":"x"}A text-only element becomes a string
<a id="1">x</a>{"a":{"@id":"1","#text":"x"}}Text next to attributes goes in #text
<a><b/><b/></a>{"a":{"b":["",""]}}Repeated siblings become an array
<a><![CDATA[1 < 2]]></a>{"a":"1 < 2"}CDATA is merged into plain text
<a/>{"a":""}An empty element becomes an empty string, not null

Two rules cause most surprises. A single child stays an object, but two or more become an array, so code that reads the JSON should accept both. All values stay strings, because XML does not say whether 42 is a number.

Details and limits

  • There is no fixed size limit: everything runs on your device, so very large inputs depend on its memory.
  • It works in current browsers on phones, tablets and computers, and it is free with no account needed.
  • Values are carried across without losing anything in ordinary data; the points above list the cases the two formats handle differently.
  • Whitespace at the start and end of an element's text is trimmed, and whitespace-only text between child elements is ignored.
  • Namespace prefixes are kept as part of the key (soap:Body) and xmlns declarations appear as ordinary @ attributes.
  • Comments and processing instructions are not part of the result.
  • Input that is not valid XML stops the tool, and the status line shows where the problem is. The XML Validator page lists the usual causes.

Questions

Why is a list sometimes an object and sometimes an array?

XML does not say whether an element may repeat. One child element becomes an object, two or more with the same name become an array. Code that reads the JSON should accept both.