Convert XML to JSON, or JSON back to XML. The mapping is the one most libraries use, and it exists because XML has three things JSON does not: attributes, mixed content and repetition. Attributes become keys prefixed with @ so they cannot collide with a child of the same name; an element that has both attributes and its own text keeps that text under #text; and repeated tags become an array, since keeping only the first would silently throw away the rest. Parsing is strict — a mismatched tag gives you an error rather than a quietly repaired structure that does not match your source. Going the other way, the JSON needs exactly one top-level key, because XML allows exactly one root element. Bulk files and folders are supported, and nothing is uploaded.
| XML | JSON |
|---|---|
| <a>t</a> | "a": "t" |
| <a x="1"/> | "a": { "@x": "1" } |
| <a x="1">t</a> | "a": { "@x": "1", "#text": "t" } |
| <r><i>1</i><i>2</i></r> | "r": { "i": ["1", "2"] } |
| <a/> | "a": "" |
No. Conversion happens in your browser.
As keys prefixed with @. So <book id="1"> becomes {"@id": "1"}. The prefix exists so an attribute cannot collide with a child element that happens to share its name — without it, one would overwrite the other.
Because XML has no way to say 'this is a list' — a list of one looks exactly like a single element. Two or more repeated tags become an array; one stays a plain value. If your consumer always expects an array, normalise it after conversion.
An element that has attributes as well as its own text needs somewhere to put the text, because the object is already holding the attributes. An element with only text and no attributes collapses to a plain string instead, which keeps the common case readable.
Most likely it has more than one top-level key, or it is an array. XML allows exactly one root element, so there is no correct way to write two — picking one arbitrarily would lose data, so it is refused with a count instead.
It is rewritten — spaces become underscores, and a leading digit gets a prefix. XML tag names cannot start with a digit or contain spaces, so emitting them verbatim would produce markup no parser accepts.
XML is read with the browser's own DOMParser in XML mode, which is strict — an unclosed tag or a mismatched one produces a parser error that is surfaced rather than swallowed, unlike HTML mode, which would silently repair the markup and give you a structure your source does not actually have. The mapping to JSON follows the convention most libraries settled on, because XML has three things JSON does not: attributes, mixed content and repetition. Attributes become keys prefixed with @, so they cannot collide with a child element of the same name. An element with children and no attributes collapses to just its text, which keeps the common case readable rather than burying every leaf under a text key. When an element carries both attributes and text, the text moves to #text, since there is nowhere else for it to go. Repeated child tags become an array — the only honest mapping, because keeping the first would silently discard the rest, and XML has no marker distinguishing a list of one from a single element, which is why a one-item list comes back as a plain value. Going the other way, JSON must have exactly one top-level key, because XML allows exactly one root element; more than one is rejected with a count rather than an arbitrary choice. Values are escaped for the five XML entities, and a key that is not a legal tag name — one with spaces, or a leading digit — is rewritten rather than producing markup no parser will accept. Everything runs in your browser.