JSON to XML Converter
Build an XML document from JSON, with attributes, repeated elements and escaping handled.
About JSON to XML Converter
Build an indented XML document from JSON: keys become elements, arrays become repeated elements, and an @attributes object becomes XML attributes.
How to use
- Paste JSON, or load a
.jsonfile (up to 5 MB) with the file picker or by dropping it on the page. - The XML appears as you type. A JSON syntax error, or a key that cannot be an XML name, is explained under the input.
- Copy the XML, or Download it as
.xmlnamed after the loaded file.
The page opens on JSON → XML. Switch the direction to XML → JSON, or to Auto, to go the other way.
How JSON becomes XML
- Root element. XML needs exactly one root. If the JSON is an object with a single key, that key is the root. Otherwise (several top-level keys, or an array) everything is wrapped in
<root>, and the items of a top-level array become<item>elements. - Arrays repeat the element once per item:
"tag": ["a", "b"]gives<tag>a</tag>and<tag>b</tag>. - Attributes. An
@attributesobject inside an element is written as attributes on its opening tag. A#textkey gives the element its text when it also has attributes. - Empty values.
nulland an empty object become a self-closing element such as<note/>. - Escaping.
&,<,>,"and'in text and attribute values are written as entities. - Names. A key that is not a valid XML name, such as one containing a space or starting with a digit, is reported as an error instead of being changed silently.
- The output starts with
<?xml version="1.0" encoding="UTF-8"?>and is indented two spaces. Namespaces need no special handling: a key such assoap:Bodyis written as it is.
Example
{
"note": {
"@attributes": { "id": "1" },
"to": ["Ada", "Grace"],
"body": "Tea & biscuits"
}
}
→
<?xml version="1.0" encoding="UTF-8"?>
<note id="1">
<to>Ada</to>
<to>Grace</to>
<body>Tea & biscuits</body>
</note>
Privacy
The conversion runs in the page. A loaded file is read locally, no request is made, and nothing is stored.
Frequently asked questions
- How do I add XML attributes from JSON?
- Put them in an object under the key @attributes inside the element, for example {"price": {"@attributes": {"currency": "GBP"}, "#text": "9.99"}}, which gives <price currency="GBP">9.99</price>. JSON has no attributes of its own, so every converter needs a convention like this one.
- How are JSON arrays written in XML?
- XML has no array type, so an array becomes the same element repeated, once per item, under the name of its key. An array at the very top of the document has no key, so its items are written as <item> elements inside <root>.
- Why is my output wrapped in a root element?
- A well-formed XML document has exactly one root element. JSON with several top-level keys, or a top-level array, would produce several, so they are placed inside <root>. To choose the name yourself, wrap the JSON in an object with one key, such as {"catalogue": …}.
- Which JSON keys are not valid XML element names?
- An XML name must start with a letter, an underscore or a colon, and may then contain letters, digits, hyphens, underscores, colons and full stops. Keys with spaces, keys that start with a digit such as "2024", and keys with characters like $ or / are rejected, and the error names the key so you can rename it.