About XML to JSON Conversion
XML and JSON solve overlapping problems with different priorities. XML was designed as a self-describing markup language with strong tooling for schemas and namespaces, and it survives in places where stability matters more than novelty: SOAP services, RSS and Atom feeds, sitemaps, Office Open XML documents, configuration files for legacy enterprise software, and government data exchanges. JSON, in contrast, is the native shape of JavaScript and the default for modern REST APIs. Translating between them is one of the most common interoperability tasks in web development — and the tricky part is not parsing, it is choosing how to represent attributes, mixed content, and repeated elements in JSON.
Three Mapping Strategies
Compact
Attributes go under @ prefixed keys (e.g. @id), text content goes under #text, and repeated child elements automatically become arrays. This is the convention used by xml2js, Badgerfish, and many backend XML libraries — best when you want the most idiomatic JavaScript object.
Verbose
Every node becomes an explicit { name, attributes, children } object. Order is preserved, comments can be kept, and there is zero ambiguity about what came from where. Best for fully lossless round-trips and when you need to reconstruct identical XML.
Simplified
Elements that contain only text and no attributes collapse directly into string values. This is the cleanest output for typical config or data XML and the easiest to consume from JavaScript, but attribute information on those leaf nodes is lost.
Quick Example
<book id="b1" lang="en">
<title>The Pragmatic Programmer</title>
<author>David Thomas</author>
<author>Andrew Hunt</author>
</book>
Compact output:
{
"book": {
"@id": "b1",
"@lang": "en",
"title": "The Pragmatic Programmer",
"author":["David Thomas", "Andrew Hunt"]
}
}
How It Works
The parser is a hand-written XML tokenizer that walks the input character by character, tracking element nesting, attribute parsing, and special blocks (CDATA, comments, processing instructions, DOCTYPE). It builds an intermediate AST that is then serialized into JSON using the strategy you select. The reverse direction analyzes the JSON shape — keys starting with @ become attributes, #text becomes text content, and arrays expand into repeated elements. All entity references (&, <, >, ", ', numeric &#NN; and &#xNN;) are decoded on parse and re-encoded on emit. Everything happens client-side via JavaScript.
Frequently Asked Questions
XML is still common in legacy APIs, SOAP services, RSS feeds, configuration files, and government data formats. JSON is the native data structure for JavaScript and the dominant format for modern REST APIs. Converting makes XML data trivially consumable by any modern frontend, Node.js service, or Python script.
Compact puts attributes under @-prefixed keys, text under #text, and merges repeated child elements into arrays — matches xml2js conventions. Verbose produces an explicit { name, attributes, children } structure with no ambiguity. Simplified collapses text-only elements into bare strings, producing the cleanest JavaScript-friendly output but losing attributes on those nodes.
CDATA sections are preserved as text content. Comments are dropped by default (JSON has no comment syntax) but kept in verbose mode. Namespace prefixes like soap:Envelope are kept verbatim as part of element and attribute names — the tool does not perform namespace resolution but does not break on prefixed names either.
Verbose mode is fully lossless. Compact mode loses comments and the order between attributes versus children. Simplified mode additionally loses attributes on text-only elements. The reverse direction reconstructs valid XML from any of the three formats as long as the JSON matches the chosen strategy.
No. The parser, serializer, and reverse converter all run entirely in your browser using JavaScript. Nothing is sent to any server, no analytics observe input, and the page works offline once loaded.