What does a JSON to XML converter do?
JSON is how modern APIs speak; XML is how many older and regulated systems still listen: SOAP endpoints, banking and payment gateways, government and tax portals, ERP and legacy integrations, Android resources, sitemaps. When a JSON payload has to reach one of them, somebody has to turn objects and arrays into elements and attributes. This page does that in your browser, without uploading the JSON.
The mapping is a convention, because the formats model data differently. JSON is made of objects, arrays, strings, numbers, booleans and null. XML is a tree of named elements with attributes and text, and it requires exactly one root element. Here, each key becomes an element, an array becomes the same element repeated, a key that starts with @ becomes an attribute, and #text becomes the text of an element that also has attributes. If the JSON has no single root key, the converter adds <root> around it.
This is the same convention the XML to JSON converter reads, so a document that goes JSON to XML and back returns to the same shape, with one caveat: XML stores numbers and booleans as text, so the type is not part of the XML.
How to convert JSON to XML
- Paste the JSON in the input box. The page opens with a sample object and the direction (JSON to XML) is fixed at the top. The JSON must be strict.
- The XML appears next to it as you type. The result starts with the
<?xml version="1.0" encoding="UTF-8"?>declaration. - Choose the Indent: 2 spaces, 4 spaces or a tab. Tick Minify to get the whole document without line breaks or indentation, which is what you want for a request body.
- Tick Sort keys to sort the keys alphabetically at every level, which also reorders the resulting elements.
- Press Copy or Download to save
converted.xml. - If a key is not a valid XML name, or the JSON is broken, a red box says which one and where.
JSON to XML examples
Attributes and repeated elements
Input:
{"catalog":{"@updated":"2024-05-01","book":[{"@id":"b1","title":"Dom Casmurro","author":"Machado de Assis","price":29.9},{"@id":"b2","title":"Grande Sertão","author":"Guimarães Rosa","price":39}],"note":null}}Output:
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" updated="2024-05-01">
<book id="b1">
<title>Dom Casmurro</title>
<author>Machado de Assis</author>
<price>29.9</price>
</book>
<book id="b2">
<title>Grande Sertão</title>
<author>Guimarães Rosa</author>
<price>39</price>
</book>
<note xsi:nil="true"/>
</catalog>The two objects of the book array became two <book> elements, @id became an attribute, and the null was written as xsi:nil="true", which tells XML Schema-aware readers the value is nil. That is how a null differs from an empty string, which becomes <b></b>.
No single root: <root> is added
{"name":"Ana","age":30,"tags":["a","b"]} has three keys, so the result is:
<root>
<name>Ana</name>
<age>30</age>
<tags>a</tags>
<tags>b</tags>
</root>A JSON array at the top, such as [1,2,3], is wrapped as <root> with one <item> per value.
Text with an attribute, and escaping
{"price":{"@currency":"BRL","#text":"19.90"}} becomes <price currency="BRL">19.90</price>. Special characters are escaped for you: {"txt":"a & b < c > d"} gives <txt>a & b < c > d</txt>.
Minified output and indentation
{"a":{"b":1}} with Minify gives <a><b>1</b></a>. With 4 spaces, <b> sits four spaces inside <a>; with a tab, one tab.
A list that needs a parent
{"root":{"m":[1,2]}} gives one <root> with two <m> children. Putting the list directly under the only top-level key, as in {"m":[1,2]}, would be two <m> elements with no parent, so the converter adds <root> around them for you. A list inside a list becomes <item> children, and an empty list is written as an empty element.
Common errors and how to fix them
- "first name" cannot be an XML element name: XML names may contain letters, digits, underscore, hyphen and dot, and must start with a letter or underscore. Rename keys like
first nameor2fa(for examplefirst_name,twofa). - "x y" cannot be an XML attribute name: the same rule applies to keys that start with
@. - Quoted object key expected but got '}' (line 1, column 8): a trailing comma in the JSON, which strict JSON forbids.
- Object value expected after ':' (position 6 in
{"a": undefined}):undefined,TrueorNaNare not JSON. Usenull,trueor a number. - Missing elements in the output: an empty array such as
"a": []produces no element at all, since XML has no way to write "zero repeats". An empty object{}becomes an empty element,<b/>.
Frequently asked questions
How do I convert JSON to XML online?
Paste the JSON, check the XML and press Copy or Download. It runs in your browser, free and without sign-up. For the reverse direction, use XML to JSON.
How are JSON arrays written in XML?
XML has no array type, so the element is repeated once per item: "tags":["a","b"] becomes two <tags> elements. Readers that need to know it is a list must be told by a schema or by convention.
How do I add attributes?
Give the key a name that starts with @, inside the object that should carry it: {"item":{"@sku":"A1","qty":2}} gives <item sku="A1"><qty>2</qty></item>. Use #text for the element's own text.
Can I validate the result?
This page checks that names are valid, but it does not validate against a schema (XSD). Run the output through your schema validator, and tidy it with the XML formatter if you need to read it. For the data in other formats, see the data converter.
Do numbers and booleans keep their type?
No, XML has no types of its own: 19.9 and true are written as text, and the receiving system decides how to read them (often with a schema). Converting back with XML to JSON guesses numbers and booleans again.
How to convert JSON to XML in code
JavaScript
fast-xml-parser also builds XML; attribute keys follow a prefix you choose.
import { XMLBuilder } from 'fast-xml-parser';
const builder = new XMLBuilder({ ignoreAttributes: false, attributeNamePrefix: '@', format: true });
console.log(builder.build({ order: { '@id': '1042', item: [{ qty: 2 }, { qty: 1 }] } }));Python
xmltodict.unparse is the inverse of parse and uses the same @ and #text convention. It needs a single root key.
import xmltodict
data = {"order": {"@id": "1042", "item": [{"qty": "2"}, {"qty": "1"}]}}
print(xmltodict.unparse(data, pretty=True))unparse adds the <?xml version="1.0" encoding="utf-8"?> line, which this page does not.
Bash
python3 -c 'import sys,json,xmltodict; print(xmltodict.unparse(json.load(sys.stdin), pretty=True))' < order.json