Producing valid XML from JSON
Plenty of systems still require XML: payment gateways, government portals, e-invoicing standards, feeds and older enterprise software. Converting JSON to XML sounds simple, but XML has rules JSON does not: a single root element, names that cannot start with a digit or contain spaces, and a clear distinction between attributes and child elements.
This converter handles those rules for you. It adds an XML declaration, wraps the data in a root element when needed, repeats elements for arrays, turns keys that start with @ into attributes and cleans up names that XML would reject.
JSON vs XML at a glance
| Feature | JSON | XML |
|---|---|---|
| Full name | JavaScript Object Notation | Extensible Markup Language |
| Type | Hierarchical data | Hierarchical markup data |
| Introduced | Early 2000s (RFC 4627 in 2006) | 1998 |
| Created by | Douglas Crockford; now ECMA-404 and RFC 8259 | W3C |
| Structure | Nested objects and arrays | Nested elements with attributes |
| Comments | No | Yes, with <!-- --> |
| Data types | String, number, boolean, null, object, array | Text, unless an XSD schema defines types |
| Best for | APIs, web apps and app settings | Enterprise data exchange, feeds and office files |
| Support | Every programming language | Every language; common in enterprise systems |
| MIME type | application/json | application/xml |
How to convert JSON to XML
Step 1: Choose a JSON file
Drop a .json file onto the converter.
Step 2: XML is preselected
Leave the target set to XML.
Step 3: Convert
The JSON is mapped to elements and attributes and pretty-printed with two-space indentation.
Step 4: Download the XML
Save the file and validate it against your schema if the receiving system has one.
Conversion rules
Single root guaranteed
An object with one top-level key uses it as the root; anything else is wrapped in <root>.
Arrays repeat elements
{"tag": ["a", "b"]} becomes two <tag> elements.
Attributes from @ keys
{"@id": 5} becomes id="5" on the parent element.
Safe names
Invalid characters become underscores and names starting with a digit get a leading underscore.
When JSON to XML is needed
Integrations with XML APIs. Prepare request bodies for SOAP or XML-based services.
Feeds and sitemaps. Generate XML structures from JSON data.
E-invoicing and filings. Draft XML that you then align with the required schema.
Legacy imports. Feed data to software that only reads XML.
How the root element is chosen
If your JSON is an object with exactly one key, such as { "catalog": { ... } }, that key becomes the root element. If the object has several top-level keys, or its only value is an array, everything is wrapped in a <root> element so the document stays valid.
A top-level array becomes <root> containing one <item> element per entry. Rename root and item in your editor if your target system expects different names.
- { "catalog": {...} } gives <catalog> as root.
- { "a": 1, "b": 2 } gives <root><a>1</a><b>2</b></root>.
- [ {...}, {...} ] gives <root><item>...</item></root>.
Round-tripping with XML to JSON
The @ prefix for attributes and #text for element text match what the XML to JSON converter produces, so you can convert XML to JSON, edit the data and convert it back.
Null values become empty self-closing elements. Special characters such as & and < in values are escaped automatically, so the output parses cleanly.
Tips for JSON to XML
- Use a single top-level key named after the root element your system expects.
- Mark attributes with @ in your JSON before converting.
- Validate the output against the XSD if one is provided.
- Avoid arrays of arrays; XML has no natural way to express them.
JSON to XML questions
Does the output include an XML declaration?
Yes: <?xml version="1.0" encoding="UTF-8"?> is added at the top.
How do I create attributes?
Prefix the key with @, for example { "book": { "@id": "42", "title": "Dune" } }.
What happens with arrays?
Each item becomes a repeated element with the array's key as its name.
What if a key is not a valid XML name?
Invalid characters are replaced with underscores, and names starting with a digit get an underscore prefix.
Is the XML indented?
Yes, with two spaces per level for readability.
Is my JSON uploaded?
No. The XML is generated in your browser.