How to Convert XML to JSON: Attributes, Arrays & Namespaces
Convert XML to JSON the right way — handle attributes, turn repeated tags into arrays, deal with namespaces, and infer types. Online, in Python, and in JavaScript.
Plenty of APIs, configs, and exports still speak XML, while the rest of your stack wants JSON. Converting XML to JSON is straightforward until you hit attributes, repeated tags, and namespaces — the three things naive converters get wrong. This guide shows how to do it cleanly, online and in code.
The quickest way: convert XML to JSON online
Copy your XML, paste it into the XML to JSON converter, and press Convert to JSON. The parser maps elements, attributes, and text into formatted JSON you can copy or download.
Prefer a tool that runs client-side (ours does) so your file isn’t uploaded — a meaningful difference from server-based converters when the XML contains private data. Local processing also removes the file-size cap.
Use it when: you want a fast, private conversion on any device.
Decide how attributes map
XML attributes have no direct JSON equivalent, so a convention is needed. The common one — used by our tool and libraries like fast-xml-parser — prefixes attribute keys with @:
<book id="1" lang="en">Working with JSON</book>
becomes:
{ "book": { "@id": 1, "@lang": "en", "#text": "Working with JSON" } }
Element text lives under #text when the element also has attributes or children. If you don’t need attributes, a good converter lets you ignore them for a cleaner result.
Repeated tags should become arrays
This is the classic gotcha. Consider:
<book><tag>json</tag><tag>xml</tag></book>
The two <tag> elements should become a JSON array:
{ "book": { "tag": ["json", "xml"] } }
But there’s an ambiguity: if a book has only one tag, a naive converter produces a string, not an array — so code that expects a list breaks. The fix is a force-array option that always emits arrays for chosen elements, giving you a predictable shape regardless of count.
Namespaces, CDATA, and types
Three more details separate a good conversion from a broken one:
- Namespaces. Prefixes like
x:noteshould be preserved in the key, not silently dropped. - CDATA.
<![CDATA[ ... ]]>content must be read as plain text. - Types. Numbers and booleans arrive as text; type inference converts
42andtrueto real JSON types while leaving genuine strings alone.
Our converter handles all three.
Convert XML to JSON in Python
xmltodict mirrors the @/#text convention:
import xmltodict, json
with open("data.xml") as f:
data = xmltodict.parse(f.read()) # attributes as @attr, text as #text
print(json.dumps(data, indent=2))
Use force_list=("tag",) to always get arrays for specific elements.
Convert XML to JSON in JavaScript
fast-xml-parser is the standard in Node:
import { XMLParser } from "fast-xml-parser";
const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "@" });
const obj = parser.parse(xmlString);
Configure isArray to force specific paths into arrays.
Which method should you use?
For a quick, private conversion — especially with attributes, repeated tags, or namespaces — use the online XML to JSON converter: paste, set force-array and type options, and download. Use Python (xmltodict) or Node (fast-xml-parser) when it’s part of a pipeline. Either way, plan for attributes, arrays, and namespaces up front.
Need the reverse? See how to convert JSON to XML. Comparing formats? Read JSON vs XML vs YAML.
Ready to merge your JSON?
Combine files or snippets in your browser — free and private.
Open the merge toolKeep reading