XML to JSON
Convert XML to JSON using the browser's native DOMParser. Handles nested elements, attributes, and repeated tags.
What is XML to JSON conversion?
XML is a nested markup tree: elements, attributes, and text. JSON is objects, arrays, and scalars. Mapping XML to JSON is a convention, not a single standard. This tool’s convention is: document element as the root key, repeated sibling tags as arrays, attributes under @attributes, and character data as a string or #text when attributes or mixed children are present.
Paste an XML document and this converter walks the tree with the browser’s DOMParser (application/xml). The JSON root is a single property named after the document element. Child elements become properties. When the same tag appears more than once under a parent, those values are merged into an array — two <user> nodes become "user": [{...}, {...}]. If Include attributes is on, attributes are grouped under an @attributes object, not rewritten as @id keys. A leaf element with only text becomes a string. A leaf that also has attributes keeps the text on #text. Mixed content (text plus child elements) stores trimmed character data on #text and maps each child tag separately. Comments and processing instructions are ignored. CDATA is exposed as text. Namespaces stay in the tagName string. Invalid XML surfaces the parser’s first error line in the input header. Conversion never leaves the browser. Format the JSON afterward with JSON Formatter, or check the XML with XML Validator.
When to use this tool
- Legacy XML APIs — Turn a SOAP-style or RSS-style payload into JSON you can inspect in a modern client.
- Config files — Convert a small XML config to JSON before moving an app to JSON or YAML configuration.
- Keep or drop attributes — Uncheck Include attributes when you only care about element text, for example a list of names.
- Validate first — If DOMParser reports a parsererror, fix well-formedness in XML Validator, then convert. XML Validator
How to use the XML to JSON Converter
Paste XML
The textarea accepts a full document including an XML declaration. Sample loads a <users> list with two <user id="..."> records.
Choose attribute handling
Include attributes (on by default) writes @attributes objects. Turning it off omits attributes entirely — ids on <user> will not appear in the JSON.
Read repeated tags as arrays
Sibling elements that share a tag name are always arrays once a second copy appears. A single child stays a bare object or string, not a one-element array.
Copy or download
Copy JSON or Download writes output.json. Sample restores the demo; Clear empties both panes.
Worked examples
Repeated children with attributes on
<users>
<user id="1">
<name>Alice</name>
<email>alice@example.com</email>
</user>
<user id="2">
<name>Bob</name>
<email>bob@example.com</email>
</user>
</users>{
"users": {
"user": [
{
"@attributes": {
"id": "1"
},
"name": "Alice",
"email": "alice@example.com"
},
{
"@attributes": {
"id": "2"
},
"name": "Bob",
"email": "bob@example.com"
}
]
}
}Leaf text versus text-plus-attributes
<user id="1">Alice</user>
{
"user": {
"@attributes": {
"id": "1"
},
"#text": "Alice"
}
}Without attributes, <item>hello</item> becomes {"item":"hello"} — a string, not an object.
Mixed content
<note lang="en">Hello <em>world</em></note>
{
"note": {
"@attributes": {
"lang": "en"
},
"em": "world",
"#text": "Hello"
}
}Whitespace-only text nodes are discarded; remaining text is trimmed.
Tips
- A single child is not wrapped in an array. If you need a uniform list, include at least two siblings or normalize in code.
- All text is a string — <age>30</age> becomes "30", not 30.
- Unclosed tags fail fast; the first parsererror line is shown above the input.
- Pretty-print or minify the result in JSON Formatter.
Frequently Asked Questions
Where do XML attributes go in the JSON?
When Include attributes is on, they are grouped under "@attributes" as a string map. The converter does not prefix each attribute with @ on the parent (it will not emit "@id"). Turn the option off to drop attributes.
How are repeated tags represented?
The second sibling with the same tag name promotes the property to an array and appends. A tag that appears only once stays a single object or string.
Are comments, DTDs, and processing instructions kept?
No. DOMParser builds an element tree; comments and processing instructions are not walked. Character data from CDATA sections is treated as text.
What about namespaces?
tagName is used as the JSON key, so a prefixed name such as soap:Body stays "soap:Body". Namespace URIs on xmlns attributes appear under @attributes when attributes are included.
Is the XML uploaded?
No. DOMParser and JSON.stringify run in the browser. Internal documents never leave the device.