How to convert JSON to XML
- Paste JSON or open a
.jsonfile. The XML appears next to it as you type. - Set the root element name (or leave it empty to use the single top-level key), the attribute prefix and the indentation.
- Copy the XML or download it as an
.xmlfile.
How JSON is mapped to XML
| JSON | XML |
|---|---|
{"name": "Ada"} | <name>Ada</name> |
{"@id": "7"} | attribute id="7" on the parent element |
{"#text": "Hello"} | text content of the parent element |
{"tag": ["a", "b"]} | <tag>a</tag><tag>b</tag> |
{"note": null} | <note/> |
{"2nd key": 1} | <_2nd_key>1</_2nd_key> – invalid names are fixed |
top-level array [1, 2] | <root><item>1</item><item>2</item></root> |
&, <, > in text | &, <, > |
JSON vs XML
JSON has arrays, numbers, booleans and null; XML only has elements, attributes and text, so types are not preserved: 42 and "42" both become the text 42. XML, in turn, has attributes, namespaces and mixed content that JSON lacks. If you plan to convert the XML back to JSON later, keep the common conventions used here – @ for attributes and #text for text content – which most converters understand.
Frequently asked questions
How are JSON arrays converted to XML?
Each item becomes an element named after the array's key, so {"book": [ … ]} becomes several <book> elements. Items of a top-level array become <item> elements inside the root.
How do I create XML attributes from JSON?
Prefix the key with @: {"user": {"@id": 7, "name": "Ada"}} becomes <user id="7"><name>Ada</name></user>. You can change the prefix in the options.
What happens to keys that are not valid XML names?
Characters that XML names cannot contain, such as spaces, are replaced with underscores, and names that start with a digit or with “xml” get an underscore in front: "2nd key" becomes <_2nd_key>.
Is the generated XML valid?
Yes. Special characters are escaped, every element name is valid and the document has exactly one root element, with an optional XML declaration at the top.