XML to JSON Converter
Paste or upload an XML document and get clean, well-formed JSON instantly. Handles attributes, nested elements, repeated elements as arrays, and CDATA — all in your browser, with nothing uploaded. Copy or download in one click.
How it works
This converter is a deterministic tree transform: it reads XML shaped by the W3C XML 1.0 grammar and emits JSON shaped by RFC 8259. There is no external API and no server round-trip, and — unlike many online converters — no DOMParser dependency either: a small hand-written, well-formedness-checking parser runs the same in the browser and during server rendering, so API responses and config files never leave your device.
- Read the prolog. An optional
<?xml …?>declaration, comments, processing instructions, and a DOCTYPE are skipped. The document must then have exactly one root element (an XML well-formedness rule). - Tokenize elements and attributes. Start tags, end tags, and self-closing tags are matched; attribute values must be quoted, and a duplicate attribute name on one element is rejected. A start tag must be closed by an end tag with the same name, or the parser stops with a line/column error rather than guessing.
- Decode text. Outside CDATA, the five predefined entities (
<,>,&,",') and numeric character references such asAare decoded. CDATA content is kept verbatim, including its<and&. - Map the tree to JSON using the documented fast-xml-parser convention: attributes become keys with the configurable prefix (default
@_); a text-only element with no attributes becomes its string directly; otherwise its text moves under the configurable key (default#text). - Collapse repeated siblings. Two or more child elements with the same tag name become a JSON array in document order, so
<book/><book/>becomes"book": [ … ]. Empty elements become an empty string (or an object holding just their attributes); inter-element formatting whitespace is dropped. - Coerce values (optional). By default every leaf is a string, so the conversion is lossless. With Parse numbers/booleans on, leaves matching the JSON number grammar (RFC 8259 §6) become numbers and exact
true/falsebecome booleans. Because that grammar forbids leading-zero integers, an identifier such as0712345678stays a string, and integers past 253−1 are kept as strings to avoid precision loss. - Serialize per RFC 8259 §6–§7. Output is produced with the native
JSON.stringify, which already escapes strings and emits valid number, boolean, and null literals.
Every successful run is cross-checked: the emitted text is parsed back with the browser's standards-compliant JSON.parse, and the summary line under the output confirms it re-parses as a JSON object and reports the element and attribute counts.
Worked examples
Frequently asked questions
Sources & references
- W3C — Extensible Markup Language (XML) 1.0 (Fifth Edition)
- RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format
- fast-xml-parser — XML-to-JSON mapping conventions (@_ attributes, #text, arrays)
The grammar and mapping rules on this page were last cross-checked against the W3C XML 1.0 specification and RFC 8259 on 2026-06-29. The page is reviewed whenever a relevant standard or edge-case bug report comes in.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found an XML edge case that does not convert right, or want a new option?
Email me at [email protected] — most fixes ship within 24 hours.