How to convert XML to JSON

By

Convert an XML document to JSON in the browser, with attributes, repeated elements, and text mapped by one documented convention.

Free toolThis tutorial uses the XML to JSON Converter. It runs in your browser, so open it in another tab and follow along.

To convert XML to JSON, map the root element to one object key, prefix each attribute with @, and turn two or more elements that share a name into an array.

We’ll use the XML to JSON converter. It runs in the browser. The document is not uploaded. The URL can remember the direction, minify, and whether the XML declaration is on. It does not store the document.

There is no single standard for this mapping. The rules below are the ones this page uses, so you can see them before you trust the object.

Convert the library sample

  1. Open the XML to JSON converter.
  2. Click Load sample, or paste your own XML.
  3. Leave the direction on XML to JSON.
  4. Read the notes, then copy the JSON or download converted.json.

The sample is a library with two books. Each book has an id attribute. The year is text in the XML.

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book id="book_1">
    <title>The Hobbit</title>
    <author>J. R. R. Tolkien</author>
    <year>1937</year>
  </book>
  <book id="book_2">
    <title>Dune</title>
    <author>Frank Herbert</author>
    <year>1965</year>
  </book>
</library>

The JSON is:

{
  "library": {
    "book": [
      {
        "@id": "book_1",
        "title": "The Hobbit",
        "author": "J. R. R. Tolkien",
        "year": "1937"
      },
      {
        "@id": "book_2",
        "title": "Dune",
        "author": "Frank Herbert",
        "year": "1965"
      }
    ]
  }
}

The note is: XML declaration: version 1.0, encoding UTF-8.

1937 stays a string. The page does not infer numbers. book is an array because the name appears twice. One child with a given name stays an object, so a library with a single book would not produce an array.

Switch to JSON to XML and the same object comes back as:

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <book id="book_1">
    <title>The Hobbit</title>
    <author>J. R. R. Tolkien</author>
    <year>1937</year>
  </book>
  <book id="book_2">
    <title>Dune</title>
    <author>Frank Herbert</author>
    <year>1965</year>
  </book>
</library>

The declaration on the way back is version="1.0" and encoding="UTF-8", unless you untick XML declaration. Minify output writes no extra space. Those choices are to=xml, indent=0, and decl=0 in the URL. Files up to 1 MB open from Open file.

How XML becomes JSON on this pagePaste XML. The page maps the root element, attributes, repeated names, and text into one JSON object, and lists comments it dropped. A DOCTYPE or an unknown entity stops the conversion.YesNoPaste XMLIs there a DOCTYPEor an unknownentity?Show the errorMap elements,attributes, and textPrint JSON and thenotes

How a node is mapped

XMLJSON
Root elementThe one key of the object
AttributeA key that starts with @
Text onlyA string, not trimmed
Attributes and textThe attributes, plus #text
Empty element""
Two or more elements with the same nameAn array, in document order
One element with that nameAn object
Comment or processing instructionDropped, and counted in the notes
CDATAText
xmlns and a prefixed nameKept as written

Whitespace between child elements is dropped. If an element mixes text with children, the text is collected into #text, and that text is not ordered against the children.

A list of one does not round-trip as an array. It comes back as an object.

What the page refuses

A DOCTYPE stops the conversion. The page does not read a DTD, and it does not fetch entities. The entities it expands are amp, lt, gt, quot, apos, and numeric character references. Anything else is an error, and so is a bare &.

On the way back to XML, the JSON needs one root object with one element key. A JSON number or boolean becomes text. null becomes an empty element. An empty array writes no elements. An integer outside Number.MAX_SAFE_INTEGER was already rounded by JSON.parse, and the page says so.

Once you have JSON, the JSON formatter pretty-prints it. XML you only want indented belongs in the XML formatter. YAML uses the YAML and JSON converter. The format itself is in the introduction to JSON, one value per line is in the JSONL guide, and JSON.parse is in parsing JSON with Node.js.

Try the XML to JSON Converter

Convert XML to JSON and JSON back to XML in your browser. Attributes, repeated elements, and text follow one documented mapping.

Open the XML to JSON Converter →

  • XML Formatter & Validator: Pretty-print or minify XML and check it is well-formed — CDATA, comments, and declarations preserved exactly.
  • JSON Formatter & Validator: Validate, pretty-print, and minify JSON with clear error messages. Runs entirely in your browser.
  • YAML ↔ JSON Converter: Convert YAML to JSON and back with indent options, line-number errors, and warnings for classic YAML gotchas.

More tool tutorials →

Want me to talk about your product? You can sponsor this site.