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.
JSON repair guides
Topic hubs
- JSON Parse Errors: Read the Message, Jump to the Fix
- Fix Invalid JSON: From 'What's Wrong' to a Clean File
- JSON Formatter, Validator, Viewer: Pick the Right Tool
- Repair LLM JSON Output: Handling Almost-JSON from AI
- Privacy: JSON Tools That Don't Leave Your Browser
- JSON Interop: YAML, CSV, XML, JWT, Schema
Specific guides
- How to Decode Base64 Strings (and JWT Payloads)
- URL Encoding: Percent-Encode Query Parameters and Paths
- Convert YAML to JSON (and Avoid Indentation Errors)
- Convert JSON to CSV: Flatten an Array of Objects
- Escape JSON as a String Literal (and Decode Double-Encoded JSON)
- Fix Trailing Comma in JSON
- Fix Single Quotes in JSON
- Fix Unquoted Keys in JSON
- Repair LLM JSON Output
- Fix JSON Parse Error: Expected Property Name
- JSON vs JS Object Literal: The Key Differences
- Validate JSON Before API Requests
- JSON Formatter vs JSON Repair
- Fix JSON Unexpected Token Errors
- JSON to JavaScript Object Converter