JSON to XML Converter — Free Online
JSON to XML Converter
Paste a JSON object and the converter returns XML text. Each key that is a valid XML name becomes an element, and _attributes, _text, _cdata and _comment set attributes, text, CDATA sections and comments. The conversion is plain JavaScript that runs in your browser; the code path for this page makes no network request, so the JSON is not uploaded.
How this tool converts JSON
The converter builds an element tree from the JSON (jsonToXmlTree in src/lib/Lang/v2/json/BaseJsonService.ts) and passes it to js2xml from the xml-js package with compact: true. Four special keys control the rest:
_attributesholds the attributes of the element it sits in._textsets the element's text._cdatawrites a CDATA section._commentwrites an XML comment.
Other rules, all visible in the output:
- The output has no XML declaration. A top-level object with one key keeps that key as the root element. Anything else at the top level is wrapped in one
<root>element: several keys, a single key whose value is an array, an empty object, a top-level array or a scalar. - An array repeats its parent element once per item. An empty array writes nothing.
- An array inside an array, or a top-level array, has no element name of its own, so each of its items is written as
<item>. - A key that is not a valid XML name is written as
<item key="…">, with the original name in thekeyattribute. A name is not valid when it starts with a digit, contains a space or an@, or is empty. nulland{}become self-closing tags, such as<coupon/>.- Booleans and numbers become text. Numbers are parsed by
JSON.parsefirst, so integers above 2^53 lose precision. &,<and>are escaped in text. Double and single quotes are not.- Indentation uses the editor's indent setting, 2 spaces by default. An indent of 0 produces one line.
- Invalid JSON produces no XML. The output is a message that contains
Invalid Json:followed by theJSON.parseerror message.
Worked example
Input, the response body of an order API:
{
"order": {
"_attributes": { "id": "A-1042", "currency": "EUR" },
"customer": { "name": "Zoë O'Neil", "email": "zoe@example.com", "vip": false },
"items": [
{ "sku": "KB-75", "qty": 2, "price": 89.5 },
{ "sku": "MS-02", "qty": 1, "price": 24 }
],
"note": "Gift wrap <yes> & deliver after 5pm",
"coupon": null,
"tags": []
}
}Output:
<order id="A-1042" currency="EUR">
<customer>
<name>Zoë O'Neil</name>
<email>zoe@example.com</email>
<vip>false</vip>
</customer>
<items>
<sku>KB-75</sku>
<qty>2</qty>
<price>89.5</price>
</items>
<items>
<sku>MS-02</sku>
<qty>1</qty>
<price>24</price>
</items>
<note>Gift wrap <yes> & deliver after 5pm</note>
<coupon/>
</order>The items array became two <items> elements. The empty tags array is missing from the output, while coupon: null became <coupon/>. The apostrophe and the "ë" pass through unchanged.
Gotchas
1. Empty arrays disappear. Input {"a":{"b":[],"c":{}}} gives the output below. The b element is not written at all, while the empty object c becomes <c/>. If the element must exist, put a value in it, or write null and accept <b/>.
<a>
<c/>
</a>2. A key that is not a valid XML name becomes an item. Input {"user":{"@id":"7"}} gives <item key="@id">, not an attribute. Use _attributes for attributes, as in {"user":{"_attributes":{"id":"7"},"name":"A"}}, which gives:
<user id="7">
<name>A</name>
</user>3. Large integers lose digits. The number 12345678901234567890 is written as 12345678901234567000, because JSON.parse rounds it before the XML is built. Store IDs as strings to keep them exact, so "12345678901234567890" gives <id>12345678901234567890</id>.
4. A top-level array gets a root and item elements. Input [{"id":1}] gives:
<root>
<item>
<id>1</id>
</item>
</root>Put the array under a named key, such as {"items": [...]}, if you want a repeated element with that name.
FAQ
Does the output include an XML declaration or a root element?
It has no declaration. The root is the one top-level key, or <root> when the data needs a wrapper. The declaration (<?xml version="1.0"?>) must be added by hand.
Can I set the root element name?
Not in this tool. The root is either your single top-level key or <root>, so rename the key in your JSON to choose the name.
How do I write attributes?
Put them under _attributes in the object that represents the element. Keys beginning with @ are not treated as attributes. They become <item key="@…"> elements.
Is the output checked for validity? No. Paste the result into the XML Formatter or the XML Validator to check it for well-formedness.
Does the output follow an XSD schema? The converter does not read schemas. Element order follows the key order of your JSON object, so match the order the schema expects.