Convert JSON to XML: Root Elements, Attributes, and Arrays

XML needs a single root and represents lists as repeated elements. Map @-prefixed keys to attributes and #text to element text for a reversible conversion.

Choosing a root element

XML allows exactly one root. If the JSON has a single top-level key, use it as the root element; otherwise wrap everything in a synthetic root element.

Attributes and text

Keys prefixed with @ become attributes and a #text key becomes the element’s text content, matching the standard XML-to-JSON convention so the conversion round-trips.

Arrays become repeated elements

A JSON array does not become one element — each item becomes a separate element with the same tag name, because XML represents lists as repetition.

Escaping

Escape ampersand, less-than, and greater-than in text (and double quotes inside attributes), and prepend the XML declaration so the output is a complete document.

Namespaces and prefixes

XML namespaces appear as xmlns or xmlns:prefix attributes and qualify element and attribute names. In a JSON-friendly form, treat them as ordinary @xmlns / @xmlns:prefix attribute keys and keep prefixed names like soap:Envelope as the key — most converters preserve the prefix verbatim.

Self-closing vs explicit empty elements

An empty JSON value maps to either a self-closing tag (<note/>) or a paired empty tag (<note></note>). Both are equivalent XML, but downstream parsers occasionally mis-handle one form — prefer self-closing for compact output and explicit pairs when the consumer is known to be strict.

Round-trip considerations

Some XML facts have no JSON equivalent: attribute order, comments, processing instructions, and the difference between an empty element and a missing one. If you convert to JSON, modify, and convert back, expect those artifacts to be dropped — never use round-tripped XML where signature canonicalisation matters.

Format XML, beautify XML, validate XML

The same converter doubles as an xml formatter, xml beautifier, and xml validator: paste any XML, click To JSON to validate well-formedness, then click To XML to re-emit with consistent xml indentation. It is the simplest way to format xml without installing a local tool — useful when an editor lacks an xml viewer or pretty-print plugin.

See also

This guide handles one format boundary. The hub lists every JSON ↔ neighbor-format conversion with its standard and edge cases.