Skip to content
jsonbeautifiers
English

JSON to XML

Well-formed XML with a chosen root, attributes and proper escaping.

JSON
XML

Nothing you paste leaves your browser. The connect-src allowlist makes that a browser guarantee rather than a promise. Check it yourself

Convert JSON to well-formed XML with a chosen root element, attribute support and correct escaping.

The two data models do not line up exactly, so this conversion makes choices. They are listed here and reported in the output notes when they apply to your document.

How the mapping works

Object keys become elements
A key of "name" becomes a <name> element.
A key prefixed with @ becomes an attribute
{"@id": "1"} becomes id="1" on the enclosing element.
A key of #text becomes the element text
Which is how you produce an element that has both attributes and content.
Arrays repeat the element
That is how XML expresses a list. There is no separate wrapper element unless you build one.
null becomes xsi:nil
The XML Schema convention. An empty element would be indistinguishable from an empty string.

Key names that are not valid XML names

XML element names cannot start with a digit and cannot contain spaces or most punctuation. JSON keys routinely do both. Such keys are rewritten with underscores, and the conversion tells you it happened rather than doing it quietly, because converting back will not restore the original names.

What XML has that JSON does not

Namespaces, processing instructions, a schema language that predates JSON Schema by a decade, comments, and a distinction between attributes and child elements. None of it survives a round trip through JSON, so a JSON to XML conversion produces valid XML that is structurally simpler than XML written by hand.

How to do this in code

Converting in code.

py Python
import json
from dicttoxml import dicttoxml

xml = dicttoxml(json.loads(text), custom_root='root', attr_type=False)

# attr_type=False turns off the type="str" attributes dicttoxml
# adds to every element by default.
js JavaScript
import { XMLBuilder } from 'fast-xml-parser';

const builder = new XMLBuilder({
  format: true,
  attributeNamePrefix: '@',
  ignoreAttributes: false,
});
const xml = builder.build(JSON.parse(text));
cs C#
using Newtonsoft.Json;

XNode node = JsonConvert.DeserializeXNode(json, "root");
var xml = node.ToString();

Questions

Why is there an xsi:nil attribute?
Because XML has no null. The XML Schema convention marks an element as nil with that attribute, which keeps null distinguishable from an empty string. An empty <field/> could be either.
Can I round-trip JSON to XML and back?
For documents that stay inside this mapping, yes. Key names that had to be rewritten will not come back, and neither will the distinction between a one-element array and a single value, since both produce one element.