XMLBeautify XML Formatter

XML to JSON: The Mapping Rules and What Gets Lost

XMLBeautify Guides · Updated 2026-10-30 · All guides

There is no standard XML-to-JSON mapping. The converter uses a common one, and this page states it so you can predict the output.

The rules

XMLJSON
Element with only textA string: <n>Dune</n> becomes "n": "Dune"
AttributeA key prefixed with @: <b id="1"/> becomes {"@id": "1"}
Element with attributes and textText goes in #text
Empty elementnull
Repeated sibling elementsAn array, in document order
Root elementThe single top-level key

All values are strings. XML has no number or boolean type, so <qty>2</qty> becomes "2". Converting types is your decision, because only you know whether 007 is a number.

The single-versus-array trap

One <item> becomes an object; two become an array. Code that reads doc.order.item.length works on orders with two items and breaks on orders with one. Handle it at the consumer: wrap a value in an array if it is not one.

What a conversion cannot keep

  • Order of mixed content. In <p>one <b>two</b> three</p>, the text pieces are joined into one #text, so where <b> sat is lost.
  • Comments and processing instructions. They are dropped.
  • Namespace meaning. Prefixes stay in key names (soap:Body) and xmlns declarations appear as @xmlns:soap attributes, but nothing resolves them.
  • Element versus attribute. <a b="1"/> and <a><b>1</b></a> differ in XML and differ in the output (@b versus b), so a consumer that accepts both forms must handle both.
  • Whitespace. Text is trimmed, so a value that was only significant whitespace becomes null.

Not well-formed input

The converter refuses to produce JSON for a malformed document and reports the line and column instead of emitting a partial result. See well-formed versus valid.

Developer Sponsor / Partner
Copied to clipboard!