← ihatecalc

XML to JSON Converter

Runs in your browser using its built-in XML parser. Nothing is uploaded.

How the XML to JSON conversion works

XML and JSON describe data differently, so a converter has to follow some conventions. This tool uses the common ones: each element becomes a key, attributes become keys starting with @ (you can turn them off), text next to child elements or attributes goes under #text, and elements that repeat under the same parent become a JSON array. An empty element becomes null.

Example

This XML:

<library city="Pune">
  <book id="1">
    <title>Clean Code</title>
  </book>
  <book id="2">
    <title>The Pragmatic Programmer</title>
  </book>
</library>

converts to:

{
  "library": {
    "@city": "Pune",
    "book": [
      {
        "@id": "1",
        "title": "Clean Code"
      },
      {
        "@id": "2",
        "title": "The Pragmatic Programmer"
      }
    ]
  }
}

Frequently asked questions

Why is one element an object but two become an array?

Repeated sibling elements become an array, while a single one stays a plain value, because XML has no way to say it is a list. If your code expects an array, wrap single values in an array after converting.

What happens to attributes?

They are kept with an @ prefix, for example "@id": "1". Untick Include attributes to leave them out.

Are numbers converted?

Not by default, so values stay as strings and nothing is changed by accident, such as leading zeros in codes. Tick Convert numbers and booleans to turn numeric text and true or false into JSON types.

What about CDATA, comments and namespaces?

CDATA is treated as text, comments and processing instructions are ignored, and namespace prefixes are kept in the key names.

Why do I get an invalid XML error?

The XML must be well formed: one root element, every tag closed and properly nested, attribute values in quotes and special characters such as & written as &. The message shows where the parser stopped.

Related tools

YAML to JSON · JSON Formatter · JSON to CSV · Base64 to JSON

Results are estimates for information only and are not financial, tax or legal advice.