JSON

XML to JSON Converter

Paste XML to get JSON. Attributes become keys starting with @, repeated elements become arrays, and numbers and true/false lose their quotes. If the XML is broken, the error names the line and column and the tag that was left open.

Runs in your browser. Nothing you enter is sent to a server.

Indent
Converts as you type.
JSON
{
  "order": {
    "@id": "INV-1042",
    "customer": {
      "name": "Ana",
      "city": "Jakarta"
    },
    "item": [
      {
        "@sku": "KB-01",
        "name": "Keyboard",
        "qty": 1,
        "price": 450000
      },
      {
        "@sku": "MS-07",
        "name": "Mouse & pad",
        "qty": 2,
        "price": 125000
      }
    ],
    "paid": true
  }
}

Root element <order> · 13 elements

Advertisements

How XML becomes JSON

XMLJSON
<name>Ana</name>"name": "Ana"
<item sku="KB-01">…</item>"item": {"@sku": "KB-01", …}
two or more <item> under one parent"item": [ … ]
<price currency="IDR">450000</price>"price": {"@currency": "IDR", "#text": 450000}
<note/>"note": ""
<![CDATA[ … ]]>plain text
<soap:Body>"soap:Body", prefix kept

The root element becomes the single top-level key. Text is trimmed at both ends. Comments, processing instructions and the DOCTYPE are left out.

When the XML is broken

The parser stops at the first problem and names its line and column, for example Expected </item> (opened on line 7) but found </order>. Common causes:

  • A bare &. In XML it must be written &amp;, also inside URLs in attributes.
  • HTML entities such as &nbsp;. XML knows only &lt; &gt; &amp; &quot; &apos;.
  • A tag closed in the wrong order, or never closed. XML, unlike HTML, has no optional end tags.
  • Two root elements. Wrap them in one parent element.

Safe with untrusted XML

The parser runs in your browser and never loads anything the XML points to: no external DTD, no external entity, no file. Custom entities declared in a DOCTYPE are not expanded, which rules out "billion laughs" style documents that grow to gigabytes.

Converting back

Click JSON → XML above to take the result to the JSON to XML Converter; the same rules run in reverse. For tables, the JSON to CSV Converter turns the JSON into a CSV file for Excel.

Questions

How are XML attributes converted?

Each attribute becomes a key starting with @, next to the child elements: <item sku="KB-01"><name>Keyboard</name></item> becomes {"item": {"@sku": "KB-01", "name": "Keyboard"}}. Namespace declarations such as xmlns:soap are attributes too, and prefixes like soap:Envelope are kept in the names.

Why is one element an array and another not?

An element that appears more than once under the same parent becomes an array. One that appears once becomes a plain value, because the XML alone does not say whether more could appear. If your code expects an array, wrap a single value in one after reading it.

What happens to text next to attributes or child elements?

It goes into a "#text" key: <price currency="IDR">450000</price> becomes {"price": {"@currency": "IDR", "#text": 450000}}. Text inside CDATA sections is treated as plain text. Comments and processing instructions are left out.

Are numbers converted?

With "Detect numbers and true/false" on, 42, 3.5, true and false lose their quotes. Values with leading zeros, such as 007 or postcodes, stay text so the zeros are kept, and numbers keep every digit. Turn it off to get every value as a string.

Why does it say "Unknown entity"?

XML knows only five named entities: &lt; &gt; &amp; &quot; and &apos;. Names such as &nbsp; or &copy; come from HTML. Type the character itself, or use its number such as &#160;. Custom entities from a DOCTYPE are not expanded, for safety.

Is my data uploaded?

No. The XML is read by a parser running in your browser, and the downloaded file is made there too. Nothing is sent to a server, and no external DTD or file referenced by the XML is ever loaded.

Support this site

The tools are free and need no account. If one saved you time, a small donation helps keep them running.

Want a tool or feature? Suggest it for free, or donate and write your request in the message. Supporters' requests are looked at first.