DevToolsmith

XML to JSON Converter

Paste XML to convert it to JSON. Attributes become @ keys, repeated elements become arrays, and numbers and booleans can be converted to real JSON types. The XML is checked for errors first, with exact line numbers.

Runs 100% in your browser — nothing is uploaded

✓ Well-formed XML

XML
Tip: drop a file here470 chars · 470 bytes · 1 line
JSON
698 chars · 698 bytes · 35 lines

Example

XML
<library name="City">
  <book id="1" available="true">
    <title>Dune</title>
    <year>1965</year>
  </book>
  <book id="2" available="false">
    <title>Neuromancer</title>
    <year>1984</year>
    <note lang="en">Signed copy</note>
  </book>
  <manager/>
</library>
JSON (numbers and booleans on)
{
  "library": {
    "@name": "City",
    "book": [
      {
        "@id": 1,
        "@available": true,
        "title": "Dune",
        "year": 1965
      },
      {
        "@id": 2,
        "@available": false,
        "title": "Neuromancer",
        "year": 1984,
        "note": {
          "@lang": "en",
          "#text": "Signed copy"
        }
      }
    ],
    "manager": null
  }
}

Conversion rules

XMLJSON
Element with only textString (or number / boolean)
Element with children or attributesObject
Attribute id="1""@id": "1"
Text next to attributes or children"#text" key
Repeated elementArray
Empty element <x/>null
CDATAIts text, unescaped
Entities like &amp;Decoded characters
Comments, processing instructionsDropped

The single-element problem

With default settings, one <book> becomes an object, not an array:

Default
{"library":{"book":{"title":"Dune"}}}
Always use arrays
{"library":{"book":[{"title":["Dune"]}]}}

If your code loops over library.book, use Always use arrays, or normalize in code with something like [].concat(library.book).

Convert XML to JSON in code

JavaScript (fast-xml-parser)
import { XMLParser } from 'fast-xml-parser';

const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@' });
const json = parser.parse(xml);
Python (xmltodict)
import json, xmltodict

data = xmltodict.parse(xml)              # attributes as "@id", text as "#text"
print(json.dumps(data, indent=2))

These libraries use the same @ and #text conventions as this converter, so the output here is a good preview of what your code will get. Need to tidy the XML first? Use the XML formatter, then explore the JSON with the JSONPath tester.

Frequently asked questions

How do I convert XML to JSON?

Paste the XML into the converter above. Each element becomes a key, attributes become keys with an @ prefix, repeated elements become arrays and text becomes the value. Turn on Numbers and booleans to convert "42" and "true" into JSON numbers and booleans.

How are XML attributes represented in JSON?

As keys with a prefix, @ by default, so <book id="1"> becomes {"book": {"@id": "1"}}. The prefix keeps attributes apart from child elements with the same name. You can choose _ or no prefix to match what your code expects.

Why is an element sometimes an array and sometimes an object?

An element that appears once becomes a single value; one that repeats becomes an array. This means the JSON shape depends on the data: a list with one item looks different from a list with two. Turn on Always use arrays to make every child element an array so your code can handle both cases the same way.

What happens to an element with both attributes and text?

It becomes an object with the attributes as @ keys and the text under #text: <price currency="USD">9.99</price> becomes {"@currency": "USD", "#text": "9.99"}. An element with only text becomes a plain string, and an empty element becomes null.

Is the conversion reversible?

Mostly, but not exactly. JSON has no comments, processing instructions or ordering between different element names, and mixed content like <p>Hello <b>world</b></p> loses the position of the text relative to child elements. For data-style XML such as API responses and config files, nothing important is lost.