How to convert JSON to XML
Updated · 3 min read
Many enterprise systems, SOAP services, government portals and accounting tools still accept only XML. This converter maps JSON to XML in a predictable way and takes care of the details that hand-written converters usually miss: escaping special characters, fixing invalid tag names and supporting attributes.
- Paste JSON or drop a
.jsonfile. - Prefix a key with
@if it should become an attribute instead of a child element. - Review the XML preview — it updates as you type.
- Copy the XML or download it as an
.xmlfile.
Example
JSON input
[
{ "@id": 101, "name": "Jane & Co", "skills": ["Go", "SQL"], "manager": null },
{ "@id": 102, "name": "Sam <Lead>", "address": { "city": "Rabat" } }
]XML output
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item id="101">
<name>Jane & Co</name>
<skills>Go</skills>
<skills>SQL</skills>
<manager/>
</item>
<item id="102">
<name>Sam <Lead></name>
<address>
<city>Rabat</city>
</address>
</item>
</root>The @id keys become attributes, the array becomes repeated <skills> elements, null becomes an empty element, and & and < in the names are escaped so the result is valid XML.
Your data stays on your device. The conversion runs in JavaScript inside this tab — files and pasted text are never uploaded to DataSwitch Pro.
Mapping rules
- The document root is
<root>; each record of a JSON array becomes an<item>. - Object keys become child elements; nested objects become nested elements.
- Keys starting with
@become attributes; a#textkey becomes the element's text. - Arrays become repeated elements with the same name.
nullbecomes a self-closing element such as<manager/>.
Invalid tag names are fixed
JSON keys can contain anything; XML names cannot. Spaces and symbols are replaced by underscores (first name → first_name), and names that start with a digit or with "xml" get a leading underscore (2024 → _2024). This guarantees the output always parses.
Escaping
The characters &, <, >, " and ' are escaped in text and attributes, and control characters that XML 1.0 forbids are removed. The file declares UTF-8 encoding, so international text needs no further escaping.
Round trips
The XML to JSON converter uses the same @attribute and #text conventions, so you can convert JSON to XML and back without losing structure. Click the swap button to try it.
Convert JSON to XML in code
For one-off files the tool above is quickest. To automate the same task in a script or pipeline, these snippets are a good starting point.
Python (xmltodict)
import json
import xmltodict # pip install xmltodict
with open("data.json", encoding="utf-8") as f:
data = json.load(f)
print(xmltodict.unparse({"root": {"item": data}}, pretty=True))
Node.js (fast-xml-parser)
// npm install fast-xml-parser
import { XMLBuilder } from "fast-xml-parser";
const builder = new XMLBuilder({ ignoreAttributes: false, attributeNamePrefix: "@", format: true });
const xml = builder.build({ root: { item: data } });
Frequently asked questions
Can I change the root and item element names?
The converter uses <root> and <item>. To use your own names, rename them in the output, or wrap your JSON in an object whose keys are the names you want.
How do I create attributes?
Prefix the key with @. For example {"@currency": "EUR", "#text": 12} becomes <price currency="EUR">12</price> when nested under a price key.
Is the output valid XML?
Yes. Names are sanitised and all text is escaped, so the document is always well-formed. Validating against a specific XSD schema is up to the receiving system.
Does it add an XML declaration?
Yes, every document starts with <?xml version="1.0" encoding="UTF-8"?>.
Is my data uploaded?
No. The XML is generated in your browser.