XML to JSON Converter
What is XML to JSON Conversion?
XML to JSON converts XML markup into a JSON structure so data can be handled more easily in JavaScript, APIs, no-code tools, or modern data pipelines. The mapping is not always obvious because XML has elements, attributes, text nodes, namespaces, ordering, and repeated nodes, while JSON uses objects, arrays, and simple values. This tool supports legacy integrations, SOAP responses, sitemaps, RSS feeds, configuration files, and test data. For production interfaces, attribute handling, empty elements, mixed content, namespaces, and array detection should be reviewed carefully. Successfully parsed JSON does not automatically mean every piece of business information was preserved in the intended shape.
How to Use
How to use
- Paste or enter XML data in the left input box
- Select indent size (2 spaces, 4 spaces, or Tab)
- Converted JSON displays on the right with syntax highlighting
- Review the conversion result and fix any XML errors
- Click Copy or Download to save the result
Data Shape Notes
- XML attributes, repeated nodes, namespaces, and mixed text can map to JSON in several valid ways; inspect the generated structure before using it.
- For API migration, define a field mapping rule instead of relying on one sample conversion.
Use Cases
Technical Principle
Conversion starts with the browser's built-in XML parser. `new DOMParser().parseFromString(xmlString, 'application/xml')` returns a `Document` that follows W3C DOM Level 3 Core, validates well-formedness against XML 1.0 (Fifth Edition), and exposes the result as a tree of `Element`, `Attr`, `Text`, `CDATASection`, and `Comment` nodes. If the input is malformed the parser does not throw — it returns a document whose root element is `<parsererror>` containing the location and reason, so the converter checks `doc.querySelector('parsererror')` first and surfaces that message instead of producing partial JSON. The tree-to-JSON mapping walks the document depth-first and follows a Badgerfish-style convention. Each element becomes a property whose key is the local name; child elements become nested objects; element attributes are flattened with a `@` prefix (e.g. `<user id="7">` → `{ "user": { "@id": "7" } }`); text content sitting alongside child elements is stored under `#text`. Repeated sibling elements with the same name fold into a JSON array: two `<item>` nodes become `"item": [ ... ]`, but a single `<item>` stays a plain object — this is the well-known "single vs many" ambiguity, because XML cannot distinguish a one-element list from a scalar value while JSON treats `"item": {...}` and `"item": [{...}]` as different shapes. Several XML features have no clean JSON equivalent and are intentionally lossy. Element order is preserved by XML but not guaranteed by JSON object key order across all consumers (although ES2015 specifies insertion order for string keys, schemas like JSON Schema do not constrain it). Mixed content like `<p>Hello <b>world</b>!</p>` interleaves text with elements and cannot round-trip into the simple key-value model. XML namespaces in Clark notation (`{http://example.com/ns}user`) reduce to the local name `user`, which can collide with an unrelated element of the same name. CDATA blocks like `<![CDATA[<raw>]]>` are unwrapped into plain text. Processing instructions (`<?xml-stylesheet ...?>`), comments (`<!-- ... -->`), and the XML declaration are discarded because JSON has no syntax for them. Indentation is applied via `JSON.stringify(value, null, indent)` with 2 spaces, 4 spaces, or `'\t'`.
- Parser: `new DOMParser().parseFromString(xml, 'application/xml')` per W3C DOM Level 3 Core; errors surface as a `<parsererror>` root element, not a thrown exception.
- Mapping conventions: Badgerfish-style `@attr` for attributes, `#text` for mixed text, element local name for children; Parker drops attributes, Spark keeps them in `$`.
- Repeated children with the same name fold into a JSON array; a single child stays an object — the classic "single vs many" ambiguity has no XML-side fix.
- Lossy features: attributes (without `@` prefix), namespaces (`xmlns:foo`), processing instructions, comments, the XML declaration, and CDATA wrappers.
- Mixed content (`<p>Hello <b>world</b>!</p>`) cannot round-trip into key-value JSON; element order may be lost across consumers that do not preserve key order.
- Number/boolean coercion is a deliberate choice: `<age>30</age>` may become `30` (number) or `"30"` (string); leading zeros (`007`) should stay strings.
- Output formatting uses `JSON.stringify(value, null, indent)` with indent of 2 spaces, 4 spaces, or `'\t'`; the result is UTF-8 with no BOM.
Examples
Simple Element
<root>
<name>John</name>
</root>
→
{"root": {"name": "John"}}Repeating Elements
<root>
<item>A</item>
<item>B</item>
</root>
→
{"root": {"item": ["A", "B"]}}Nested Structure
<root>
<user>
<name>John</name>
<age>30</age>
</user>
</root>
→
{"root": {"user": {"name": "John", "age": 30}}}FAQ
How are XML elements mapped to JSON?
Each element becomes a JSON property whose value is the element's content (string or nested object). Repeated elements with the same name become arrays. Attributes are usually prefixed with '@' or stored under a special key like '$'. The exact convention varies and is configurable on most pages.
Why is XML-to-JSON sometimes lossy?
XML distinguishes elements, attributes, text content, mixed content (text and elements), namespaces, processing instructions, and comments. JSON has only objects, arrays, strings, numbers, booleans, and null. Mapping the richer XML tree onto JSON loses information about ordering and the element-vs-attribute distinction.
How are mixed-content elements (text + child elements) handled?
Hello world!
doesn't have a clean JSON representation. The page typically uses a special key (e.g. '#text' or '$') to capture loose text, but order between text and elements is not always preserved. Avoid mixed content if you plan to round-trip.What about XML namespaces?
Prefixed elements (<ns:foo>) become string keys 'ns:foo' or are split into ns/local-name properties. xmlns declarations may be preserved as attribute-like properties or dropped, depending on the page's configuration.
Can I get JSON back to identical XML?
Round-trip is not lossless in general, especially for mixed content, comments, processing instructions, and attribute order. For data-only XML (configuration, simple records), round-trip usually works. For document-style XML (DocBook, HTML, RSS), use a real XML parser.
Is empty XML element <foo/> the same as <foo></foo>?
Yes for the parser. Both produce a property foo with an empty string or null value. Self-closing syntax is purely cosmetic in XML.
Is the conversion local?
Yes. XML parsing and JSON generation happen in your browser. The page does not upload your data.