How to convert XML to JSON
Updated · 3 min read
XML is still everywhere — SOAP APIs, RSS feeds, sitemaps, invoices, bank files and office documents — while modern code prefers JSON. This converter parses the XML with the browser's own standards-compliant parser, checks that it is well-formed, and maps elements, attributes and text to JSON using the same conventions as popular libraries such as Python's xmltodict.
- Paste XML or drop an
.xmlfile onto the input box. - If the document is malformed, the error and its line and column are shown.
- Leave Detect numbers & booleans on to get typed values, or turn it off to keep all text as strings.
- Copy or download the JSON.
Example
XML input
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101" lang="en">
<title>XML Developer's Guide</title>
<price currency="USD">44.95</price>
<available>true</available>
</book>
<book id="bk102" lang="fr">
<title>Midnight Rain</title>
<price currency="EUR">5.95</price>
<available>false</available>
</book>
</catalog>JSON output
[
{
"@id": "bk101",
"@lang": "en",
"title": "XML Developer's Guide",
"price": {
"@currency": "USD",
"#text": 44.95
},
"available": true
},
{
"@id": "bk102",
"@lang": "fr",
"title": "Midnight Rain",
"price": {
"@currency": "EUR",
"#text": 5.95
},
"available": false
}
]The repeated <book> elements become an array of objects. Attributes are prefixed with @, and an element that has both attributes and text — like <price> — keeps its text under #text.
Your data stays on your device. The conversion runs in JavaScript inside this tab — files and pasted text are never uploaded to DataSwitch Pro.
Conversion rules
- Elements become keys; their text becomes the value.
- Attributes become keys prefixed with
@, for example"@id": "bk101". - Text plus attributes: the text is stored under
#text. - Repeated sibling elements become an array.
- Lists: when the root (or a single wrapper element) contains repeated children, the result is a JSON array of those records. Other documents are converted to a single JSON object, so nothing is lost.
- Types: plain numbers and true/false become JSON numbers and booleans; codes with leading zeros stay strings.
Namespaces, CDATA and comments
Namespace prefixes are kept in the key names ("dc:creator"). CDATA sections are treated as normal text, so HTML embedded in an RSS description arrives as a plain string. Comments and processing instructions are not data and are dropped.
Validation
Before converting, the document must be well-formed: every tag closed, properly nested, attributes quoted, special characters such as & escaped. If it is not, the converter shows the parser's message with the line and column of the first problem — handy for debugging XML generated by other systems.
Need a table instead?
If your XML is a list of records — products, orders, feed items — the XML to CSV converter flattens it into rows and columns, and XML to Excel produces a ready-to-open workbook.
Convert XML to JSON in code
For one-off files the tool above is quickest. To automate the same task in a script or pipeline, these snippets are a good starting point.
Python (xmltodict)
import json
import xmltodict # pip install xmltodict
with open("data.xml", encoding="utf-8") as f:
data = xmltodict.parse(f.read())
print(json.dumps(data, indent=2, ensure_ascii=False))
Node.js (fast-xml-parser)
// npm install fast-xml-parser
import { XMLParser } from "fast-xml-parser";
const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "@" });
const json = parser.parse(xmlString);
Frequently asked questions
Why is one element an object and another an array?
XML does not say whether an element can repeat. A single child becomes an object; two or more become an array. If your code needs a consistent shape, normalise the field to an array after conversion.
Can I keep all values as strings?
Yes. Untick Detect numbers & booleans in Options.
Is the conversion lossless?
Element names, attributes, text and nesting are preserved. Comments, processing instructions and the order of mixed text between elements are not represented in JSON.
Does it support large XML files?
Files of several megabytes convert in seconds. Because XML is parsed with the browser's built-in parser, it runs on the main page rather than in a background thread.
Is my XML uploaded?
No. It is parsed locally by your browser.