XML to JSON Converter

In short: paste XML, get JSON. Attributes become keys starting with @_, repeated elements become arrays, text content stays a string, and CDATA and entities are handled properly. The conversion runs entirely in your browser with a parser built for this page — your XML is never uploaded, which matters when it is a config file or a data export.

The conversion rules

XML and JSON do not map onto each other perfectly — XML has attributes, mixed content and namespaces that JSON has no native place for — so every converter picks conventions. This one uses the widely understood set (the same family as the popular xml2js and fast-xml-parser defaults): an element with only text becomes a string; attributes are kept as keys prefixed with @_ so gives "@_id": "b1"; child elements become keys; a child tag that appears more than once becomes an array, in document order; an element that has both attributes or children and text keeps its text under "#text". Comments, processing instructions and the XML declaration are dropped, CDATA sections are read as text, and entity references (&, <, numeric references) are decoded. Numbers stay strings — "29.99" does not silently become 29.99 — because XML itself has no number type and guessing types corrupts data like leading-zero codes.

Two consequences to know before you build on the output. A child that appears once is an object, and twice is an array — so code consuming the JSON must handle both shapes for any repeatable element, or you must normalise afterwards. And mixed content (text interleaved with elements, as in HTML paragraphs) loses its ordering: the text chunks are joined under #text and the elements sit beside them. For data-shaped XML — feeds, configs, exports, API responses — neither issue bites. For document-shaped XML, no converter can save you; the formats genuinely model different things.

Why it runs in your browser

XML files are frequently sensitive: configuration files with hostnames and structure, order exports, medical and financial data feeds, API responses captured while debugging. Uploading one to a stranger's server to change its punctuation is a bad trade. This converter is a hand-written parser delivered as part of the page — paste, convert and copy happen on your machine, and closing the tab destroys the only copy. It also means the tool works offline, on a plane, inside a locked-down network, at the speed of your own processor. Malformed XML is reported with the parser's complaint and the character position, which is usually enough to find the unclosed tag or the raw ampersand (the commonest XML error of all: an & in text must be written &).

A worked example

The sample in the box: a bookstore with two books. Converted, it gives a "bookstore" object whose "book" key is an array of two objects — because the tag repeats. The first book carries "@_id": "b1" and "@_lang": "en" from its attributes, plus "title" and "price" strings; the entity in "Data & Design" arrives decoded. Had there been only one book, "book" would be a single object, not an array of one — the convention consequence noted above, and the thing to check first when consuming code misbehaves on small inputs. Try deleting the second book and converting again to watch the shape change; that experiment teaches the convention faster than any documentation.

Frequently asked questions

How are XML attributes converted to JSON?

They become keys prefixed with @_ inside the element's object. <book id="b1" lang="en"> becomes {"@_id": "b1", "@_lang": "en", ...}. The prefix prevents collisions with child elements of the same name — an attribute id and a child <id> can coexist — and it matches the convention used by common libraries, so output from this tool reads the way users of xml2js or fast-xml-parser expect. If an element has attributes and text, the text is kept under the "#text" key.

Why did my element become an array — or not?

Because repetition decides the shape. A tag that appears once under its parent converts to a single value; the same tag appearing twice or more converts to an array, in document order. That is the standard convention, and the standard gotcha: code written against a two-item example breaks on a one-item input. Robust consumers wrap the value: if it is not already an array, make it a one-element array before looping.

Are numbers converted to JSON numbers?

No — every text value stays a string. XML has no number type: <price>29.99</price> and <code>007</code> are both just text, and a converter that guesses would turn the second into 7 and destroy the data. Keeping strings is the lossless choice. If your pipeline needs numbers, convert the specific fields you know are numeric on the JSON side, where the schema is yours.

What happens with CDATA, comments and namespaces?

CDATA sections are read as plain text (their content is not parsed for entities, exactly as XML specifies). Comments and processing instructions, including the <?xml?> declaration, are dropped — JSON has nowhere to keep them. Namespace prefixes are kept as part of the key name, so <media:content> becomes a "media:content" key and xmlns declarations appear as "@_xmlns:media" attributes. Nothing is silently renamed.

Is my XML uploaded to a server?

No. The parser is JavaScript delivered with this page and the conversion happens entirely in your browser — no upload, no storage, no analytics on content. That makes the tool safe for config files, data exports and captured API responses, and it works offline once the page has loaded. If the XML is malformed, the error message and character position are shown so you can fix the source rather than wonder.

Related tools

Parser verified on 24 November 2026 against a test suite of XML documents (attributes, repeats, CDATA, entities, mixed content, malformed input). Everything runs in your browser — nothing you type is sent anywhere.