Skip to main content

XML to JSON

Convert XML to JSON, keeping its attributes.

XML input

Ln 1, Col 1
XML

Result

Your result will appear here

Add XML above, then choose Convert.

Add XML on the left, then choose Convert.

Waiting for input

2 spaces

Convert XML to JSON, with attributes as @name keys and repeated elements gathered into arrays so the structure carries over.

What is XML to JSON?

XML to JSON is a free online tool that converts XML into JSON in your browser, keeping its structure: the root element becomes the top-level name, attributes become names that start with @, and elements that repeat become a list.

It reads one well-formed XML document and refuses one that is not, telling you where the first problem is. It never loads anything the XML refers to.

What it supports

  • One well-formed XML document with a single root element. The XML declaration, comments and processing instructions are accepted and left out of the JSON.
  • The name of the root element is the only top-level name of the result, as in {"library": …}. Namespace prefixes stay in the names (n:child), and namespace declarations are written as attributes (@xmlns:n).
  • An element with only text becomes a JSON text. An element with no text, no attributes and no children becomes null.
  • Attributes become names that start with @, written first and in the order they appear.
  • Text that sits beside attributes or child elements goes under #text. Text is trimmed at both ends, and pieces of text separated by child elements are joined with one space. CDATA sections are read as plain text, and the five built-in entities (&, <, >, " and ') and numeric references such as A are decoded.
  • Child elements with the same name become a list, placed where the first of them appears. An element that appears once stays a single value. Elements are grouped by name, so siblings that alternate, such as a b a, come out as one list a followed by b.
  • Every value is text, because XML has no types: 12.50 and true stay text.

Good to know

  • The conversion cannot be reversed with certainty. XML does not say whether one item is a list of one or a single value, so an element that appears once is a single value and only repeated elements become a list. Code that reads the JSON should accept both.
  • Comments, processing instructions and the XML declaration are dropped, and so is the position of text among child elements.
  • A DOCTYPE is read but never followed. A custom entity it declares is not expanded, and a reference to such an entity is refused instead of left in the text. Nothing the DOCTYPE points to is loaded.
  • The XML must be well-formed: matching tags, one root, quoted attributes, no stray < or &. A document is never repaired, and the first problem is reported with its line and column. Several elements side by side with no common root are refused.
  • Namespaces are checked but not interpreted. A prefix must be declared, but it stays part of the name, so two prefixes for the same namespace give two different names.

How do I use XML to JSON?

  1. Paste or type your XML in the input box. Nothing runs while you type.
  2. Choose the indentation (“2 spaces” or “4 spaces”) and press “Convert”.
  3. Read the JSON next to your input. If the XML is not well-formed, or the conversion is refused, the message says why and gives the line and column.
  4. Press “Copy” to put the JSON on your clipboard, or “Download” to save it as converted.json.

Examples

Elements and attributes

Paste this XML and press “Convert”. The root element, library, becomes the only top-level name. The two book elements have the same name, so they become a list. The attributes id and lang become the names @id and @lang, written before the child elements, and the text of title and author becomes a plain text.

<library>
  <book id="1" lang="en">
    <title>Dune</title>
    <author>Frank Herbert</author>
  </book>
  <book id="2" lang="en">
    <title>Emma</title>
    <author>Jane Austen</author>
  </book>
</library>
{
  "library": {
    "book": [
      {
        "@id": "1",
        "@lang": "en",
        "title": "Dune",
        "author": "Frank Herbert"
      },
      {
        "@id": "2",
        "@lang": "en",
        "title": "Emma",
        "author": "Jane Austen"
      }
    ]
  }
}

A closing tag that does not match

The element b is still open when the closing tag </a> arrives, so the XML is not well-formed and nothing is converted. The message gives the line and column of the closing tag.

<a><b></a>

This closing tag does not match the element that is open. Closing tags must be identical to the opening tags, including upper and lower case, and must close in reverse order.

Line 1, column 7

Text next to attributes, and empty elements

The text of total sits beside an attribute, so it moves to a name called #text, and total becomes an object with @currency and #text. The empty element note becomes null, and the attribute id of order is written as @id before the children.

<order id="7"><total currency="BRL">12.50</total><note/></order>
{
  "order": {
    "@id": "7",
    "total": {
      "@currency": "BRL",
      "#text": "12.50"
    },
    "note": null
  }
}

Text mixed with elements

When an element holds both text and child elements, the pieces of text are joined with one space under #text, and the child elements keep their own names. The position of the text between the children is not kept.

<p>Hello <b>big</b> world</p>
{
  "p": {
    "#text": "Hello world",
    "b": "big"
  }
}

An entity from a DOCTYPE

The DOCTYPE declares an entity called who. The tool reads the DOCTYPE but never follows it, so it does not expand &who; and refuses instead of leaving it out. Only the five built-in entities (&amp;, &lt;, &gt;, &quot; and &apos;) and numeric references such as &#65; are expanded.

<!DOCTYPE note [<!ENTITY who "Ada">]>
<note>Hi &who;</note>

This entity is not one of the five predefined XML entities (&amp;, &lt;, &gt;, &quot; and &apos;), and this tool does not expand entities declared in a DOCTYPE. Use one of the five or a numeric character reference.

Line 2, column 10

Limits and privacy

Size limits

  • Input: up to 2,000,000 bytes of UTF-8 text (about 2 MB). Accented letters and emoji take more than one byte each. Larger input is rejected.
  • Result: also up to 2,000,000 bytes. If the complete result would be larger, the tool shows only an incomplete preview of the first 100,000 bytes, and that preview cannot be copied or downloaded.
  • Nesting: up to 128 levels of elements (the root is level 1; self-closing elements count too) inside each other. Deeper XML is refused because it is beyond what the tool processes.
  • Time: a run that takes longer than its time limit is stopped. Try a smaller input.

Is my text sent to a server?

No. The text you type or paste, or open from a local file, is processed locally in your browser, in a dedicated Web Worker. It is never uploaded or sent to the server (a file you open is read in your browser only), and it is not written to storage, cookies or the address bar.

“Copy” puts the result on your clipboard and “Download” saves it as a file, but only when you press the button. The file is created in your browser, so nothing is uploaded.

For the full details, see the Privacy Policy

Related tools and pages

Your text stays in the page when you switch tools in the same tab, so you can try the same text in another tool. Reloading or closing the tab ends it.

All tools