JSON appears here.How to use
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.
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.
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
- Checks that the XML is well-formed first and highlights the line of any problem.
- Attribute prefix, text key, namespace, CDATA and comment options match what your code expects.
- Force arrays for every element or for named ones, so the JSON shape stays stable.
- Keeps leading zeros as strings, and works on pasted, uploaded or URL-loaded XML in your browser.
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.
| XML | JSON |
|---|---|
| <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.