XML to JSON
Convert XML to JSON with @attributes, #text and arrays for repeated tags
About XML to JSON
Parses XML with your browser's built-in DOMParser and maps it to JSON: attributes become @name keys, repeated sibling elements become arrays, and text goes under #text when an element also has attributes or children. Values stay strings unless you turn on number and boolean parsing. Comments are dropped and text is trimmed, so it fits data feeds, SOAP responses and config files better than prose documents.
Category: Converters.
Frequently asked questions
How are attributes, text and repeated elements mapped?
▾
Each attribute becomes a key prefixed with @, such as @id, and namespace prefixes stay in the names, so you get keys like soap:Body and @xmlns:soap. An element with only text becomes a plain string; text next to attributes or child elements goes under #text, and siblings with the same name are grouped into an array.
Why are numbers and booleans strings in the JSON?
▾
XML has no data types, so every value is kept as text by default and nothing changes. With Parse numbers and booleans on, true, false and numbers that read back identically, like 42 or -3.5, become JSON values, while 007, 1.50 and very long integers stay strings to keep their exact text.
What does Always use arrays do?
▾
It wraps every child element value in an array, even when the element appears only once, so code that loops over items doesn't break on a list with a single entry. The root element is never wrapped.
What is lost in the conversion?
▾
Comments, processing instructions and the XML declaration are dropped, leading and trailing whitespace in text is trimmed, and the order between differently named siblings is not kept. In mixed content such as <p>Hi <b>there</b>!</p>, the text pieces are merged into one #text value. HTML entities like are rejected because XML only predefines five.