JSONConverter
โ† Back to the tools
๐Ÿ—‚๏ธ

XML to JSON: Parse Elements, Attributes and CDATA

XML to JSON parses any well-formed XML document into a clean JSON tree: elements become objects, repeated tags become arrays, attributes appear under @_ keys and element text under #text. Validation runs first, so malformed XML is reported with a line/column error instead of producing garbage.

Elements and nesting become objects

<root><a>1</a></root> becomes {"root":{"a":1}}. Element text that is numeric or boolean is typed, so <age>30</age> becomes the number 30.

Repeated elements become arrays

Two <item> tags become {"item":[1,2]}. A single item stays a scalar โ€” the JSON matches the XML shape exactly.

Attributes and text are namespaced

An element with an attribute and text, <a id="7">txt</a>, becomes {"a":{"@_id":"7","#text":"txt"}}. Attributes stay strings; element text coerces to numbers and booleans.

CDATA and namespaces are handled

CDATA sections are flattened to plain text, namespace prefixes are stripped, and self-closing empty elements become empty strings. The parser is the battle-tested fast-xml-parser, not a hand-rolled regex.

Validate before you trust

The converter validates the document first and returns a precise error (line and column) for mismatched tags or stray text. Paste any XML into the free XML to JSON converter to try it.

Convert your data free