U.UtilityApp
🔄 ConvertersFree · No signup

JSON to XML Converter

Convert a JSON object into well-formed XML markup.

Which element becomes the root

If your JSON is an object with exactly one key, that key becomes the root element. The sample does this, and its @id key becomes an attribute:

{"note":{"@id":"1","to":"Ada","body":"Hello there"}}
<?xml version="1.0" encoding="UTF-8"?> <note id="1"> <to>Ada</to> <body>Hello there</body> </note>

Anything else gets wrapped in an element called root. That includes an object with two or more keys, and a bare string or number:

{"name":"Ada","age":36}
<?xml version="1.0" encoding="UTF-8"?> <root> <name>Ada</name> <age>36</age> </root>

Every result starts with an XML declaration naming UTF-8 and is indented with two spaces.

Arrays repeat the element name

An array doesn't get its own wrapper. Each item is written as a separate element carrying the array's key name, which is the usual XML way to show a list:

{"order":{"@id":"A17","item":[{"@sku":"X1","name":"Pen"},{"@sku":"X2","name":"Pad"}],"tags":["new","gift"]}}
<?xml version="1.0" encoding="UTF-8"?> <order id="A17"> <item sku="X1"> <name>Pen</name> </item> <item sku="X2"> <name>Pad</name> </item> <tags>new</tags> <tags>gift</tags> </order>

That rule causes trouble at the top level. A JSON array, or a single key that holds an array, produces several root elements in a row, and an XML document may only have one:

<?xml version="1.0" encoding="UTF-8"?> <root> <name>Ada</name> </root> <root> <name>Alan</name> </root>

Wrap the list in two keys, one for the container and one for each item, such as {"people":{"person":[{"name":"Ada"},{"name":"Alan"}]}}. That gives one people root holding two person elements. Nested arrays are flattened the same way: [[1,2],[3]] under one key becomes three sibling elements.

Escaping, empty values and nulls

Ampersands, angle brackets and double quotes in values are escaped. An apostrophe is left as it is, which is fine in element text. null and empty objects become self-closing tags:

{"msg":{"text":"Fish & Chips <b> \"q\"","nil":null,"obj":{}}}
<?xml version="1.0" encoding="UTF-8"?> <msg> <text>Fish &amp; Chips &lt;b&gt; &quot;q&quot;</text> <nil/> <obj/> </msg>

An empty string gives an open and close tag with nothing between them, and an empty array produces no element at all, only a blank line. Numbers and booleans are written as plain text, with no type information, so 36 and "36" give identical XML.

Key names it does not repair

JSON keys can be any string, but XML element names can't start with a digit or contain spaces. The converter copies keys as they are, so these come out broken:

<?xml version="1.0" encoding="UTF-8"?> <data> <first name>Ada</first name> <2fa>true</2fa> <#text>x</#text> </data>

A parser will reject that document. Rename such keys first, for example first_name and twoFactor. The #text key produced by our XML to JSON converter has the same problem: it is written as an element, not as the text content, so move that value into a normal key before converting back.

Frequently asked questions

How do I make a value an attribute instead of a child element?+

Start its key with an at sign, as in @id. The at sign is removed and the value is written inside the opening tag. Attribute values should be plain strings or numbers, because an object there comes out as the text [object Object].

Can I change the indentation or drop the XML declaration?+

No. The output always uses two spaces per level and always begins with the UTF-8 declaration. Delete the first line by hand if the system you are feeding doesn't want it.

Does it use CDATA sections for text with special characters?+

No. Special characters are escaped as entities instead, which any XML parser turns back into the original text.

Embed this tool on your website

Free to use on blogs, school sites and intranets. Paste this code into your page's HTML; please keep the credit line.

<iframe src="https://utilityapp.lat/embed/convert/json-to-xml" title="JSON to XML Converter" width="100%" height="600" style="border:0;max-width:760px" loading="lazy"></iframe>
<p style="font-size:13px"><a href="https://utilityapp.lat/convert/json-to-xml">JSON to XML Converter</a> by UtilityApp</p>