XML is far from dead. Product feeds for marketplaces, RSS and Atom feeds, sitemaps, SOAP web services, bank statement exports, e-invoicing formats and plenty of enterprise integrations still speak it. Meanwhile your front end, your NoSQL database and most modern APIs expect JSON. Sooner or later you need to move data from one to the other.
The conversion looks trivial until you meet your first attribute, your first element that appears once in one file and three times in the next, or a product code like 00042 that comes out as the number 42. This guide explains how XML concepts map onto JSON, where the mapping is ambiguous, and how to get output your code can rely on.
Why XML does not map neatly onto JSON
XML and JSON were designed for different things. XML is a document markup language; JSON is a data format. Four XML features have no direct JSON equivalent:
- Attributes.
<price currency="USD">24.99</price>holds two pieces of data in one element: an attribute and text content. JSON objects only have keys. - Repetition without arrays. XML has no array syntax. A list is simply the same element repeated:
<tag>a</tag><tag>b</tag>. One occurrence looks exactly like a single value. - Mixed content. An element can contain text and child elements interleaved, such as
<p>Hello <b>world</b>!</p>. JSON has no natural representation of that. - Everything is text. XML has no numbers or booleans.
<stock>140</stock>is the string "140" unless a schema says otherwise.
Every XML-to-JSON converter has to make decisions about these, so understanding the conventions is the key to predictable output.
The common mapping conventions
Most converters, including the one used here, follow these rules:
| XML | JSON |
|---|---|
<name>Wireless Mouse</name> |
"name": "Wireless Mouse" |
Attribute id="p-100" |
"@_id": "p-100" (prefixed key) |
| Element with attribute and text | { "@_currency": "USD", "#text": 24.99 } |
Repeated <tag> elements |
"tag": ["electronics", "accessories"] |
<![CDATA[Works with <b>most</b> laptops]]> |
"Works with <b>most</b> laptops" |
<?xml version="1.0"?> declaration |
dropped |
The attribute prefix exists to prevent collisions. An element can have both an attribute called id and a child element called <id>; without a prefix they would fight over the same JSON key. @_ is a common default, @ is used by several other tools, and you can drop the prefix if you know your data never collides.
Convert XML with our tool
The XML to JSON Converter runs in your browser, using the open-source fast-xml-parser library, and validates the XML before converting it.
- Open the XML to JSON Converter and paste your document into XML input, click Open file to load an
.xml,.rss,.atomor.svgfile, or try Load sample. - Choose whether to Include attributes. Turning it off gives simpler JSON but loses any data stored in attributes.
- Pick the Attribute prefix: @_ (default), @, _ or No prefix.
- Decide on Convert numbers & booleans. When on,
24.99becomes a JSON number andtruea boolean; when off, every value stays a string. - Choose the JSON indent and review the JSON output. If the XML is malformed, you get the error with its line and column instead.
- Copy the result or Download it as a
.jsonfile.
The two traps that break code
Single items are not arrays
Imagine an order feed. On Monday there are three <order> elements, and the converter gives you "order": [ ... ]. On Tuesday there is only one, and you get "order": { ... }. Code that loops over order works on Monday and crashes on Tuesday.
A converter cannot know that an element may repeat when it only sees it once. There are two fixes. In your own code, normalise:
const orders = [].concat(data.orders?.order ?? []);
That always gives an array: empty, one item or many. Alternatively, when you run the conversion in code, tell the parser which elements are always lists. With fast-xml-parser in Node.js:
const { XMLParser } = require("fast-xml-parser");
const xml = `<orders>
<order id="A-100"><sku>00042</sku><paid>true</paid></order>
</orders>`;
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: "@_",
parseTagValue: false,
isArray: (name) => name === "order",
});
console.log(JSON.stringify(parser.parse(xml)));
// {"orders":{"order":[{"sku":"00042","paid":"true","@_id":"A-100"}]}}
Type conversion eats leading zeros
With number conversion switched on, the same <sku>00042</sku> becomes "sku": 42. For a price that is what you want; for a product code, postcode, phone number or account number it silently corrupts data. The example above sets parseTagValue: false for exactly this reason.
The rule of thumb: convert types when the XML contains measurements and quantities, keep strings when it contains identifiers. In the browser tool, switch off Convert numbers & booleans for identifier-heavy data and convert the few numeric fields in your own code.
Other ways to convert XML to JSON
Command line. The Go-based yq tool reads XML and writes JSON (yq -p=xml -o=json feed.xml). It is well suited to scripts and pipelines. Check its documentation for how it names attributes, since conventions differ between tools.
Python. The standard library parses XML (xml.etree.ElementTree), but turning the tree into dictionaries is up to you. The third-party xmltodict package does the mapping in one call and uses @ for attributes and #text for text.
XSLT. For complex, repeatable transformations in XML-centric environments, an XSLT stylesheet can emit exactly the JSON structure you want. Powerful, but a steep learning curve.
Ask for JSON. Many APIs that default to XML also offer JSON via an Accept: application/json header or a format parameter. Check before building a conversion step.
Tips and troubleshooting
"Unexpected character" or unclosed tag errors
The most common cause is a bare ampersand. In XML, & must be written as & unless it is inside CDATA. Also check that every opening tag has a matching closing tag with exactly the same case.
Namespaces make keys look strange
Prefixed element names such as media:content or g:price (common in RSS and shopping feeds) are kept as the full key. Access them with bracket notation, for example item["g:price"].
Mixed content looks jumbled
Text interleaved with markup, as in an HTML-like paragraph, loses its ordering in JSON. For documents rather than data, keep XML or convert to HTML instead.
Going the other way
To produce XML from JSON, use the JSON to XML converter. To tidy the source first, use the XML Formatter. Once converted, you can inspect large results in the JSON Viewer, or extract specific values with the JSONPath Tester.
Frequently asked questions
How are XML attributes represented in JSON?
As extra keys on the element's object, usually with a prefix such as @_ or @ to distinguish them from child elements. An element with both attributes and text puts the text under a key such as #text.
Why does one element become an object and another an array?
Repeated sibling elements become arrays; a single occurrence becomes a plain value or object. Normalise in code, or configure the parser to always treat certain elements as arrays.
Is XML to JSON conversion lossless?
Not always. Comments, processing instructions, the order of mixed text and elements, and the difference between attributes and child elements may be lost or changed, depending on the options you choose.
What happens to CDATA sections?
The CDATA wrapper is removed and the content becomes an ordinary string, including any angle brackets it contains.
Can I convert an RSS feed to JSON?
Yes. RSS is XML, so paste or open the feed file. Items become an array under rss.channel.item when there are two or more, which is the typical case.