XML to JSON Converter — Attributes, Arrays and CDATA Handled
Paste XML and get readable JSON: attributes land in @attributes, repeated elements become arrays and indentation whitespace is ignored. The conversion runs entirely in your browser, so nothing is uploaded.
Related tools
- Data Converter Convert between JSON, XML, YAML and CSV formats and Unix timestamps. Format conversion with clear error messages and customizable output formatting.
- Unix Timestamp Converter Convert Unix timestamps to readable dates and dates back to epoch time, in seconds or milliseconds.
- CSV to JSON Converter Convert CSV to a JSON array of objects, with comma, semicolon or tab delimiter and the header row as keys.
- JSON to YAML Converter Convert JSON to clean YAML for Kubernetes, docker-compose or GitHub Actions, and YAML back to JSON.
Features
- Convert XML to JSON in one click or while you type
- Attributes kept in an @attributes object
- Repeated elements become JSON arrays
- Handles CDATA, entities and namespace prefixes
- Indent with 2 or 4 spaces, tabs or compact output
- Runs entirely in your browser, nothing is uploaded
When You Need XML as JSON
XML is still everywhere interfaces are old or formal: SOAP web services, RSS and Atom feeds, XML sitemaps, Maven or Android config files and exports from ERP, accounting or banking software. Modern code, on the other hand, expects JSON: JavaScript works with it natively, REST APIs speak it and document databases store it.
Converting an XML response to JSON lets you see its structure at a glance, build test fixtures or check which fields a legacy API really returns before you write a parser. Paste the XML, convert it, then copy the result or download it as converted.json.
How Elements, Attributes and Text Are Mapped
XML has concepts that JSON lacks, so every converter needs conventions. This one uses these:
- Root element: its name is dropped; its content becomes the top-level JSON object.
- Attributes go into an
@attributesobject under their exact names. - Text: an element that contains only text becomes a string. If it also has attributes, the text moves to
#text. - Repeated elements with the same name become an array; a single element stays an object.
- Whitespace used for indentation is ignored, and text is trimmed at both ends.
- CDATA is treated as normal text, entities such as
&are decoded, and comments and processing instructions are dropped. - Namespaces: prefixes stay part of the key (
soap:Body), andxmlnsdeclarations appear under@attributes.
The JSON can be indented with 2 or 4 spaces or tabs, or written compactly on one line. The root and item name fields only matter for the opposite direction, JSON → XML.
Example: A Small Book Catalog
Two book elements with attributes and a CDATA section:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="b1">
<title>XML Basics</title>
<price currency="EUR">19.90</price>
</book>
<book id="b2">
<title><![CDATA[Tips & Tricks]]></title>
<price>9.50</price>
</book>
</catalog>Result with 2-space indentation:
{
"book": [
{
"@attributes": {
"id": "b1"
},
"title": "XML Basics",
"price": {
"@attributes": {
"currency": "EUR"
},
"#text": "19.90"
}
},
{
"@attributes": {
"id": "b2"
},
"title": "Tips & Tricks",
"price": "9.50"
}
]
}The repeated book became an array, id landed in @attributes, and the price with a currency attribute keeps its value in #text. The CDATA content is now plain text.
Common Pitfalls
- One or many: a feed with a single
itemyields an object, with two it yields an array. Code that reads the JSON should handle both cases. - Everything is a string:
<age>30</age>becomes"30", not the number 30, andtruestays a string too. Without a schema, XML has no data types. - Empty elements such as
<note/>become an empty object{}. - Mixed content: in
<p>Hello <b>world</b></p>the text is collected into a single#textnext tob, and its position is lost. For HTML-like documents, JSON is the wrong target. - Invalid XML: the parser is strict. Unclosed tags, a bare
&, HTML entities such as or two root elements produce the message “Invalid XML format” without a line number. If you cannot spot the problem, run the XML through a validator first.
Useful Resources