My Tool Studio
Developer

XML to JSON Converter

This XML to JSON converter parses an XML document and returns JSON that keeps attributes, nesting and repeated elements, updating as you type. By default attributes get an @_ prefix, repeated elements become arrays, and numbers and booleans become real JSON types, while values with leading zeros stay strings. You can change the attribute prefix or drop attributes, rename the text key, force arrays for every element or for names you list, strip namespace prefixes, keep CDATA and comments, and pick the indent or minified output. The XML is checked for well-formedness first, so a broken tag is highlighted with its line and column. Paste, drop, upload or load XML from a URL, then copy or download the .json file.

Always freeNo sign upRuns in your browser
Swap: JSON to XML
JSON appears here.

How to use

01

Add XML

Paste an XML document, drop or upload a .xml file, use Load URL for a feed or sitemap, or press Try sample to load a book catalog with attributes, a comment and CDATA.

02

Choose how it maps

Pick the attribute prefix, text key and array rule, list elements that must always be arrays, and set namespace, CDATA, comment and indent options. The JSON updates as you type.

03

Copy or download

Press Copy above the JSON pane, or Download .json to save it. If the XML is broken, the error names the line and column and Go to line jumps there.

Why XML to JSON Converter

How XML maps to JSON

The table shows how each part of an XML document appears in the JSON output with the default options. The examples use the same catalog as the sample on this page.

Arrays appear only where elements repeat, so the shape of the JSON can change when a document has one item instead of two. Code that reads the output should accept both an object and an array for elements that can repeat. Attribute values go through the same number and boolean detection as element text.

XMLJSON
<title>Dune</title>"title": "Dune"
<book id="bk101">...</book>"book": { "@_id": "bk101", ... }
<price currency="EUR">9.99</price>"price": { "@_currency": "EUR", "#text": 9.99 }
Two <book> elements under one parent"book": [ {...}, {...} ]
<isbn>0441013597</isbn>"isbn": "0441013597" (leading zero kept)
<available>true</available>"available": true
<note/>"note": ""
<expr><![CDATA[a < b]]></expr>"expr": "a < b"
<dc:title>Dune</dc:title>"dc:title": "Dune"

Code equivalents in JavaScript and Python

In JavaScript, this page uses the fast-xml-parser library. new XMLParser({ ignoreAttributes: false, attributeNamePrefix: "@_" }).parse(xml) gives the same default output, and its isArray option is how the array settings here work.

In Python, xmltodict.parse(xml) gives a similar shape, but with @ for attributes and #text for text, and every value kept as a string. Pass force_list=("book",) to get the Always arrays for behaviour.

Common questions

How are repeated XML elements turned into JSON arrays?
When the same element appears more than once under one parent, the converter collects them into an array. Two <book> elements inside <catalog> become "book": [ {...}, {...} ].
Why is a single child element not an array in the JSON?
With the default Arrays setting, an array is created only when an element repeats, so a catalog with one <book> gives "book": {...}. To keep the shape stable, set Arrays to For every element, or type the element names in Always arrays for, such as book, and even a single <book> becomes a one-item array.
What happens to CDATA sections and namespaces in XML to JSON?
By default, CDATA content becomes part of the element's text with the markers removed; tick Keep CDATA as #cdata to keep it under its own key. Namespace prefixes stay in key names, such as "dc:title", unless you tick Remove namespace prefixes, and xmlns declarations appear as attributes.
How are XML attributes shown in the JSON output?
Each attribute becomes a key with the chosen prefix, @_ by default, so <book id="bk101"> gives "@_id": "bk101". Choose @, _, $ or no prefix instead, or untick Keep attributes to drop them. When an element has both attributes and text, the text goes under the text key, #text by default.
Are XML comments and the declaration kept in the JSON?
The <?xml ...?> declaration and processing instructions are always left out. Comments are dropped unless you tick Keep comments as #comment.
Why did the XML to JSON converter report an error?
The XML is not well-formed. Common causes are a tag that is never closed, tags closed in the wrong order, an attribute value without quotes, or more than one root element. The message gives the line and column to check.
Does the XML to JSON converter keep leading zeros?
Yes. Values such as 00501 or 0441013597 stay as strings, because turning them into numbers would drop the zeros. Other numeric values, like 9.99, become JSON numbers.
Can I convert the JSON back to XML?
Yes, with the JSON to XML Converter. Choose @ as the attribute prefix here, or pick the same prefix in both tools, and attributes survive the round trip.
Is my XML uploaded when I convert it to JSON?
No. Upload .xml opens the file inside your browser tab, and the parsing happens there. Nothing is sent to a server.

More Developer tools

View all