How to Convert JSON to XML (with Attributes)
JSON is what most web services, apps and modern tools send and save. But plenty of systems still expect XML: product feeds, shop and accounting imports, older business software, SOAP services and many configuration files. Converting the JSON saves you from building the XML by hand.
This guide shows how each piece of JSON turns into XML and how to convert with the free JSON to XML converter, including attributes, lists and names XML doesn’t allow.
How JSON turns into XML
- Each key of an object becomes an element with that name, and its value becomes the element’s text.
- A list (array) repeats its element once per item.
nullbecomes an empty element such as<subtitle/>;trueandfalseare written as text.- Everything goes inside one root element, because an XML document must have exactly one.
- Characters with a meaning in XML are escaped:
&becomes&and<becomes<. Numbers are copied exactly, so24.50keeps its zero.
Here’s a small shop catalogue in JSON:
{
"name": "Corner Books",
"books": [
{
"@id": "bk101",
"title": "XML & JSON for Everyone",
"price": { "@currency": "EUR", "#text": 29.95 },
"tags": ["data", "web"]
},
{
"@id": "bk102",
"title": "Daten für alle",
"price": { "@currency": "EUR", "#text": 24.50 },
"in stock": false,
"subtitle": null,
"tags": []
}
]
}and the XML it becomes, with catalog as the root element:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<name>Corner Books</name>
<books id="bk101">
<title>XML & JSON for Everyone</title>
<price currency="EUR">29.95</price>
<tags>data</tags>
<tags>web</tags>
</books>
<books id="bk102">
<title>Daten für alle</title>
<price currency="EUR">24.50</price>
<in_stock>false</in_stock>
<subtitle/>
</books>
</catalog>Keys starting with @ became attributes (id="bk101"), and #text became the text of <price>, next to its currency attribute. The key in stock can’t be an XML name because of the space, so it became in_stock, and the empty tags list produced nothing. Notes under the result tell you about both.
Tip: the list name is used for every item, so "books" gives <books> for each book. If you want the usual <book>, name the list "book" in your JSON.
How to convert JSON to XML
- Open the JSON to XML converter and paste your JSON into JSON input, click Open file, or drop a .json file onto the box. Sample loads an example. A mistake in the JSON is shown with its line and column.
- Type a name in Root element, such as
catalog,productsor whatever the receiving system expects. - Choose the other options, explained below.
- Click Convert to XML, then Download the .xml file or Copy it.
The options
- Root element. The name of the element around everything. It’s
rootuntil you change it. - Indentation. 2 spaces, 4 spaces or a tab, to make the XML easy to read. It doesn’t change the data.
- Arrays. Repeat the element (the default) gives
<tags>data</tags><tags>web</tags>, as in the example. Wrap items keeps the list as one element and puts each entry in an<item>; an empty list then stays as an empty element:
<catalog>
<name>Corner Books</name>
<books>
<item id="bk101">
<title>XML & JSON for Everyone</title>
<price currency="EUR">29.95</price>
<tags>
<item>data</item>
<item>web</item>
</tags>
</item>
<item id="bk102">
…
<tags/>
</item>
</books>
</catalog>- @keys become attributes. On by default. Turn it off and
@idand#textbecome ordinary elements, with a_in front because XML names can’t start with@or#:
<books>
<_id>bk101</_id>
<title>XML & JSON for Everyone</title>
<price>
<_currency>EUR</_currency>
<_text>29.95</_text>
</price>
…- XML declaration. The first line,
<?xml version="1.0" encoding="UTF-8"?>. Keep it for files; leave it out when the XML is pasted into a bigger document.
When the JSON is a list
If your JSON starts with [, there are no keys to name the records after, so each one becomes an <item> inside the root element:
[
{ "name": "Ada", "city": "London" },
{ "name": "Alan", "city": "Manchester" }
]<?xml version="1.0" encoding="UTF-8"?>
<people>
<item>
<name>Ada</name>
<city>London</city>
</item>
<item>
<name>Alan</name>
<city>Manchester</city>
</item>
</people>To get <person> instead, wrap the list in an object first: { "person": [ … ] }.
Names XML doesn’t allow
JSON keys can be anything, XML names can’t. They may not contain spaces or most punctuation, may not start with a digit, and may not start with xml. The converter fixes such keys for you: other characters become _, and a name that starts wrongly gets a _ in front, so 2024 becomes _2024. A colon is replaced too, so the result never contains namespaces by accident. Control characters that XML doesn’t allow at all are removed from the text, and a note says how many.
More tools
- XML to JSON: the other way round. See how to convert XML to JSON.
- XML Validator: check the result against the XSD schema of the system you import into.
- JSON Formatter: find and fix mistakes in the JSON first.
Is my JSON uploaded?
No. The conversion runs in your browser, so your data never leaves your device. Our server only records that one conversion happened, for the daily limit.
Frequently asked questions
Will converting back to JSON give me the same file?
Close, but not always exactly. XML has no numbers, true or false, or lists: everything is text, and a list with one item looks like a single element. Keep the original JSON if you need it.
Can I add a namespace?
Not in the converter: colons in keys are replaced to keep the XML valid. Add the namespace to the root element afterwards, such as <catalog xmlns="https://example.com/catalog">.
Why is a key missing from the XML?
It was probably an empty list, which produces no element with Repeat the element. Choose Wrap items to keep it as an empty element.