🔄 XML to JSON Converter
By ToolNimba Web Dev Team · Updated 2026-06-19
Paste XML above, then press Convert to JSON.
Paste any XML document and this converter turns it into clean, pretty-printed JSON in your browser. It uses the browser's built-in XML parser to read the tree, then maps element names to JSON keys, collects attributes under a prefix you choose, and folds repeated sibling tags into arrays. Nothing is uploaded: the conversion runs entirely on your device, so it is safe for configuration files, API payloads and private data.
What is the XML to JSON?
XML and JSON both describe structured, hierarchical data, but they model it differently, so converting between them always involves a few conventions. XML has named elements, attributes, text content, comments and CDATA, while JSON has only objects, arrays, strings, numbers, booleans and null. There is no single official mapping, which is why this tool spells out exactly what it does rather than leaving you guessing.
The core rules are simple. Each XML element becomes a JSON key whose name matches the tag. An element that contains only text becomes a string. An element that has attributes or child elements becomes an object. Attributes are written into that object under a prefix (the default is the @ sign, so id="5" becomes "@id": "5"), which keeps them clearly separate from child elements. When an element has both attributes and text, the text is stored under a "#text" key alongside the attributes.
The trickiest case is repetition. XML happily repeats the same tag (three author elements inside one book, for example), but a JSON object cannot have the same key twice. The standard fix, and the one used here, is to collapse repeated sibling tags into an array. A single occurrence stays a plain value, while two or more occurrences of the same tag become a JSON array. This is the one behaviour worth knowing about, because a record that sometimes has one child and sometimes many will switch between a value and an array depending on the input.
When to use it
- Turning an XML API response or SOAP payload into JSON so a JavaScript or front-end app can consume it.
- Inspecting a dense XML config or RSS feed in a more readable, indented JSON shape.
- Migrating data from an XML export into a tool, database or script that expects JSON.
- Teaching or learning how XML elements, attributes and text map onto JSON objects and arrays.
How to use the XML to JSON
- Paste or type your XML into the input box (it must have a single root element).
- Choose the attribute prefix (default @) used to label attributes in the JSON.
- Leave Trim whitespace on to drop indentation-only text nodes, or turn it off to keep raw text.
- Press Convert to JSON, then use Copy to grab the pretty-printed result.
Formula & method
Worked examples
A simple note with one attribute and three text-only children.
- Input: a note element with id="1" containing to, from and body elements.
- The note element has an attribute, so it becomes an object.
- The id attribute becomes "@id": "1" (using the default @ prefix).
- Each child (to, from, body) holds only text, so each becomes a string key.
Result: { "note": { "@id": "1", "to": "Ada", "from": "Grace", "body": "Hello" } }
A book element that repeats the author tag twice.
- Input: a book element containing one title and two author elements.
- title appears once, so it stays a plain string value.
- author appears twice, so the two values collapse into a JSON array.
- The result nests both inside the book object.
Result: { "book": { "title": "The Pragmatic Programmer", "author": ["Hunt", "Thomas"] } }
How each XML construct maps to JSON in this converter
| XML construct | JSON result |
|---|---|
| Element with only text | A string value |
| Element with attributes | An object, attributes under the prefix |
| Attribute name="value" | Key (prefix + name): "value" |
| Repeated sibling tags | An array of values |
| Element with text and attributes | Object with a "#text" key plus attributes |
| CDATA section | Treated as text content |
Attribute prefix options and how an id attribute appears
| Prefix setting | JSON key for id |
|---|---|
| @ (default) | "@id" |
| $ (dollar) | "$id" |
| _ (underscore) | "_id" |
| none | "id" (may clash with a child of the same name) |
Common mistakes to avoid
- Expecting attributes and elements to look identical. Attributes are deliberately prefixed (for example "@id") so they do not collide with child elements that might share the same name. If you pick the "none" prefix and an element has both an attribute and a child called id, one will overwrite the other.
- Assuming a tag is always an array. A tag that appears once becomes a single value, while the same tag appearing two or more times becomes an array. Code that always reads it as an array can break on records that happen to have only one child. Normalise to an array in your code if you need consistency.
- Pasting an XML fragment without a single root. Valid XML needs exactly one root element. Two top-level elements with no wrapper will fail to parse. Wrap your fragment in a single parent tag before converting.
- Forgetting that numbers stay strings. XML has no type information, so a value like 42 or true is text. This converter keeps every value as a string to stay faithful to the source. Cast to numbers or booleans yourself if your code needs real types.
Glossary
- Element
- A named XML node written with an opening and closing tag, such as title or book, that can contain text and other elements.
- Attribute
- A name and value pair written inside an element opening tag, such as id="5". It describes the element rather than nesting under it.
- Root element
- The single outermost element that wraps the whole XML document. Valid XML must have exactly one.
- CDATA
- A section that holds raw character data which the parser will not treat as markup, useful for embedding symbols. It is read as plain text here.
- DOMParser
- A built-in browser API that parses an XML or HTML string into a navigable document tree, used by this tool entirely on your device.
Frequently asked questions
How does this tool convert XML to JSON?
It parses your XML with the browser DOMParser into a document tree, then walks that tree: element names become JSON keys, attributes are placed under a chosen prefix, text content becomes string values, and repeated sibling tags collapse into arrays. The whole process runs in your browser.
How are XML attributes handled?
Each attribute is written into the element object under a prefix you select (the default is @, so id="5" becomes "@id": "5"). The prefix keeps attributes clearly separate from child elements that might share the same name. You can change it to $, _, or none.
What happens to repeated tags?
A JSON object cannot repeat a key, so when the same tag appears two or more times among siblings, the values are collected into a JSON array. A tag that appears only once stays a single value, not a one-item array.
Does it convert values to numbers or booleans?
No. XML carries no type information, so every value is kept as a string to stay faithful to the source. If you need real numbers or booleans, cast them in your own code after conversion.
Is my XML data uploaded anywhere?
No. The conversion is 100% client-side using your browser built-in parser, so your XML never leaves your device or touches a server. It is safe for private config files and API payloads.
Why do I get an Invalid XML error?
The browser parser reports an error when the XML is not well formed: a missing closing tag, more than one root element, an unescaped angle bracket or ampersand, or mismatched tags. Fix the structure (and wrap fragments in a single root) and try again.