XML to JSON

Converter

Convert an XML document to JSON via DOMParser with attribute and namespace support for modernizing legacy API responses and configuration file processing.

Input → Output
XMLInput
JSONOutput

About XML to JSON

Through SOAP endpoints, RSS/Atom feeds, Android layout files, and legacy backend APIs, XML still shows up constantly — yet browsers, modern services, and frontend frameworks are far more comfortable with JSON. This tool parses XML and converts it to JSON locally in your browser, sensibly handling attributes, text nodes, namespaces, comments, and processing instructions, with options to keep or drop the namespace prefix and to preserve CDATA. Repeated same-name elements are gathered into arrays while single occurrences stay as plain objects, so the same schema can produce different shapes; keep that in mind on the consuming side. You can customize the attribute marker (default @) and the text-node key (default #text). Because everything runs on your machine, internal or sensitive messages are never uploaded. For example, <user><id>1</id></user> becomes {"user":{"id":"1"}} with attributes and children mapped to object fields, giving a clean starting point for client code. Converting XML to JSON is usually a bridge step between old and new systems, where a legacy interface offers only XML and new code understands only JSON. There is no single correct answer, because the two data models differ fundamentally: XML distinguishes attributes from text nodes, allows repeated elements without a container, and supports namespaces, while JSON has only objects, arrays and scalars, so the mapping necessarily involves choices. Several conventions are common: prefix attributes and turn them into ordinary keys, map text content to an agreed key name, and aggregate same-named repeated elements into an array. Which is better depends on the consumer, but the convention must be consistent and documented, or the same document yields different structures under different implementations, a discrepancy usually noticed only once data goes wrong. Practical notes: Before converting, establish how the target system treats a single element versus an array, since some implementations map a single same-named child to a scalar rather than an array. So the consumer suddenly receives a different type when the element count changes, which is the classic production failure. The safe convention is that such fields always map to an array. Namespaces are the other frequent problem: dropping prefixes during conversion merges same-named elements from different namespaces incorrectly, so first confirm whether the source actually uses namespaces. Limits: Comments, processing instructions and the document type declaration are necessarily lost, and information that must survive belongs in the data itself. Attributes and child elements mean different things, attributes typically metadata and elements structured data, and flattening everything into sibling keys discards that distinction, which breaks any consumer relying on it. XML also permits mixed content, where a node holds both text and child elements, with no JSON equivalent, so such documents require an explicit decision between a marked-up string and outright rejection rather than silent loss. Data and privacy: XML needing conversion often comes from enterprise interfaces and public-sector data that may contain personal information and business keys. This tool parses and converts in the browser, uploads nothing and works offline; mask personal data before processing and confirm the result does not expand sensitive attributes along with everything else.

How It Works Internally

The reverse has no single answer because the models differ fundamentally: XML distinguishes attributes from text nodes, allows repeated elements without a container and supports namespaces, while JSON has only objects, arrays and scalars. Common conventions prefix attributes, map text to an agreed key, and aggregate same-named siblings into arrays. Document the convention or the same document yields different structures per implementation.

Input<a id="1">text</a>
Output{"a":{"@id":"1","#text":"text"}} (prefixed attribute, text on an agreed key)

Measured on This Page

Fixed: mixed content loses text<x>text<y/></x> used to output {"x":{"y":""}} — the direct text "text" vanished entirely. Direct text nodes are now collected into #text
Same-name siblings become arrays<a>1</a><a>2</a> → "a":["1","2"]; a single same-name child stays an object, so consumers must handle both shapes
Attribute and text<u id="7">Bob</u> → {"u":{"@id":"7","#text":"Bob"}} — attributes carry an @ prefix, text goes to #text
Big numbers safeXML values are already strings: <n>9007199254740993</n> → "9007199254740993", no precision issue — which is the trade-off of XML→JSON keeping everything as strings

There is no single correct XML→JSON mapping; this page follows the common BadgerFish-style convention (@-prefixed attributes, #text for text). The measured defect was mixed content: for text plus child elements like <x>text<y/></x>, the old code looked only at element children and silently dropped the direct text. Also keep in mind all values are strings (XML has no type system), so <age>30</age> yields "30" not 30, and same-name siblings aggregate into an array while a single one stays an object — downstream code must accept both shapes.

How this was measured: Four XML inputs (mixed text plus child element, same-name siblings, attribute with text, big number) were converted and inspected, before and after the fix. · Measured 2026-10-31

How to Use

  1. Open "XML to JSON"
  2. Paste the XML document
  3. Attributes come out as @name fields
  4. Repeated elements merge into arrays
  5. Click the copy icon to take the JSON

Use Cases

  • Consume SOAP responses — Convert XML from a SOAP endpoint to JSON so the frontend can use JSON.parse directly.
  • Parse RSS/Atom — Convert feed XML to JSON to easily extract title, link, and date fields.
  • Migrate legacy systems — Convert legacy XML data to JSON for import into new JSON-based services.
  • Read configs — Convert XML config to JSON to avoid bundling a heavy XML parser in the frontend.
  • Analyze data — After converting to JSON, query and aggregate with jq or JSONPath.
  • Transform SVG data — Convert SVG XML markup to JSON for dynamic rendering in React or Vue components.
  • Parse EDI/XML messages — Convert EDI-standard XML to JSON objects for processing in modern data pipelines.

FAQ

Are XML attributes lost?

No. Attributes typically map to keys with a prefix (e.g. @ or $) to distinguish them from child elements, keeping all information.

How are repeated elements handled?

Repeated same-name children become a JSON array; elements appearing once become a plain object — as expected, so the consumer should handle both shapes.

What about namespaces?

Namespace prefixes are usually kept in key names (e.g. ns:tag). Clean them up manually after conversion, or use the strip-prefix option, if not needed.

What about mixed content (text + children)?

When an element has both text and child nodes, the text goes into a dedicated key (e.g. #text) while children map normally.

Which XML constructs are dropped?

Comments, processing instructions, and the XML declaration can be discarded by default; toggle them on if you must preserve narrative comments or PIs.

Can I get JSON back to XML?

Use our JSON to XML tool; note the attribute/marker conventions you configured here so a round-trip stays predictable.

Repeated XML tags become arrays but single ones become objects — how do I fix that?

This is the classic XML→JSON ambiguity: JSON needs to know upfront whether something is an array. The tool infers from occurrence count, so structures vary between documents. Normalise on the consumer side or force arrays for repeated tags in the options.

Namespaced tags (ns:tag) keep the colon in keys — why?

The tool preserves raw tag names including prefixes rather than resolving namespaces, which requires the document's xmlns declarations and can be ambiguous. Strip prefixes with a regex after conversion, or use a default namespace at the source.