AllOneTools Logo
AllOneTools

XML → JSON

Convert XML to JSON online

Transform XML documents into clean JSON. Handles attributes, namespaces, text nodes, and CDATA. In-browser, privacy-first, no upload.

Last updated:

[object Object]

[object Object]

[object Object]

Open the XML → JSON converter

Runs entirely in your browser. No signup, no upload.

Launch converter →

Example: XML → JSON

Input (.xml)

<?xml version="1.0"?>
<catalog>
  <book id="bk101" lang="en">
    <title>XML Developer's Guide</title>
    <price currency="USD">44.95</price>
  </book>
  <book id="bk102" lang="fr">
    <title>Le guide XML</title>
    <price currency="EUR">39.95</price>
  </book>
</catalog>

Output (.json)

{
  "catalog": {
    "book": [
      {
        "@id": "bk101",
        "@lang": "en",
        "title": "XML Developer's Guide",
        "price": { "@currency": "USD", "#text": "44.95" }
      },
      {
        "@id": "bk102",
        "@lang": "fr",
        "title": "Le guide XML",
        "price": { "@currency": "EUR", "#text": "39.95" }
      }
    ]
  }
}

How to convert XML to JSON manually

Prefer scripting this in a pipeline? Here are the most common CLI and library approaches.

Python with xmltodict

import xmltodict, json
data = xmltodict.parse(open('in.xml').read())
open('out.json','w').write(json.dumps(data, indent=2))

Node.js with fast-xml-parser

const { XMLParser } = require('fast-xml-parser');
const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@' });
const json = parser.parse(require('fs').readFileSync('in.xml','utf8'));

JavaScript in browser (DOMParser)

const dom = new DOMParser().parseFromString(xmlString, 'text/xml');
// then walk the DOM and build a JS object

Frequently asked questions

Why are attributes prefixed with `@`?

Because JSON has no concept of attributes — only keys and values. Prefixing attributes with `@` (the xmltodict / badgerfish convention) makes them round-trippable and visually distinct from child elements. You can toggle the prefix in the converter options.

What happens to CDATA sections?

We unwrap CDATA and emit the raw string as the text value. If the content contains characters that would otherwise need escaping (like `<`), they're passed through as-is since JSON doesn't require the XML-specific escaping.

My XML has one-or-many elements — sometimes a single `<book>`, sometimes an array. How is that handled?

By default we emit a plain object when there's one child and an array when there are multiple — matching how most parsers behave. If you need consistent arrays, enable the `Always array` option so consumers don't have to branch on type.

Are XML namespaces preserved?

Yes. Prefixed element names like `<ns:foo>` become JSON keys like `ns:foo`, and `xmlns:ns` attributes are emitted as `@xmlns:ns`. If you prefer namespace stripping, toggle `Strip namespaces` to flatten everything to local names.

Can I reverse this back to XML?

Yes, with caveats — lossy conversions (mixed content, comments, processing instructions, whitespace) can't be fully round-tripped. For the common case (attributes, elements, text), our /convert/json-to-xml/ page converts cleanly.

Related tools & guides