How to Convert JSON to XML: Attributes, Arrays and Nulls

Last updated: · 5 min read

JSON and XML describe the same data with different building blocks. JSON has objects, arrays, numbers, booleans and null; XML has elements, attributes and text. No specification defines how one should be mapped onto the other, so every library picks its own convention. That matters as soon as you have to feed a SOAP endpoint, a legacy ERP import or any system that still expects XML. This guide walks through the rules the JSON to XML Converter applies, shows what happens to arrays and empty values, and is explicit about the information that does not survive the trip. The output in the example is what the tool actually returns.

Formula

Rules:
1. The text is parsed with JSON.parse; invalid JSON is rejected together with the parser's reason.
2. The XML declaration is written on the first line and the outermost value is wrapped in the root element name you supply.
3. Every key of an object becomes a child element; strings, numbers and booleans are written as element text.
4. An array under a key is expanded into repeated elements with that key's name; items of an array without a key (at the root, or an array inside an array) are named <item>.
5. null becomes an empty element (<note/>); an empty array under a key produces no element at all.
6. A key of the form "@name" with a primitive value is written as an attribute, and "#text" as the element's text.
7. & < > are escaped (and " inside attributes); characters that are not allowed in an XML name are replaced with _, and a name that starts with a digit gets a leading _.

Input (root element name: order):
{"@id":"A-17","customer":"Kaya & Co","item":[{"sku":"P1","qty":2},{"sku":"P2","qty":1}],"tags":[],"note":null}

Output:
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-17">
  <customer>Kaya &amp; Co</customer>
  <item>
    <sku>P1</sku>
    <qty>2</qty>
  </item>
  <item>
    <sku>P2</sku>
    <qty>1</qty>
  </item>
  <note/>
</order>

There is no formula here, only a set of mapping rules. XML 1.0 requires exactly one root element and restricts the characters a name may start with; JSON (RFC 8259) has neither constraint. The tool bridges the gap by asking for a root name and sanitising keys. The "@" and "#text" notation is a widespread convention, not a standard: if the receiving system expects a different layout, shape the JSON accordingly before converting. Output is indented with two spaces.

What the inputs mean

JSON
The JSON text to convert. It may be an object, an array or a single value; keys need double quotes and trailing commas are not allowed. Up to 1,000,000 characters and 200 nesting levels are processed.
Root element name
The name of the outermost XML element. If you leave it empty, root is used. Characters that are not valid in an XML name are replaced with _.

Worked example: Turning an order into XML

  1. The input is the JSON above, with order as the root element name. Line one is the XML declaration and the outer object becomes the <order> element.
  2. The key "@id":"A-17" starts with @, so it is written as an attribute rather than a child: <order id="A-17">.
  3. The ampersand in customer is escaped: <customer>Kaya &amp; Co</customer>. Left as it is, the document would not be well-formed.
  4. The two entries of the item array are written as two consecutive <item> elements; no wrapper element is created for the array. The quantities appear as <qty>2</qty> and <qty>1</qty>, with nothing left to say they were numbers.
  5. tags is an empty array, so it does not appear in the output; note is null and becomes the empty element <note/>.

Result: The result is a 13-line XML document indented with two spaces: the root <order id="A-17">, one <customer>, two <item> elements and an empty <note/>. The tags key is absent.

🧩 JSON to XML Converter

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Treat this conversion as one-way. XML has no notion of number, boolean or string, so both 2 and "2" end up as <qty>2</qty>. A one-element array looks exactly like a single value, and an empty array vanishes. Feed the output to the XML to JSON Converter and qty comes back as the string "2", while tags does not come back at all. If the consumer validates against an XSD or expects namespaces, check the result against that schema separately: the tool does no schema validation, and because a colon in a key is replaced with _, it cannot produce prefixed names such as soap:Body. This is general information.

Common mistakes

  • Expecting an empty array to produce an empty element. "tags":[] is simply left out; if the receiver requires the element, send null instead.
  • Assuming numbers and booleans keep their type. Everything is text in XML; only a schema on the other side can restore the type.
  • Leaving a field that should be an attribute as a plain key. Unless you write "@id" instead of "id", the value becomes a child element.
  • Thinking that keys with spaces or a leading digit pass through unchanged. The key "1st item" becomes the element <_1st_item>.
  • Passing a root-level array and expecting to control the item names. The input [1,2] is written as <item>1</item> and <item>2</item> inside the root element.
  • Pasting a JavaScript object literal instead of JSON. {'a':1} uses single quotes and is rejected with an "Invalid JSON" error.

Frequently asked questions

Is there a standard way to convert JSON to XML?

No. Neither XML 1.0 nor RFC 8259 defines a mapping. Writing attributes as "@name" and text as "#text" is a common convention, and it is the one this tool follows. If the other side expects a different convention, you have to restructure the JSON first.

How do I produce an XML attribute?

Prefix the key with @ and give it a string, number, boolean or null value: {"@id":"A-17"} yields id="A-17". A "#text" key in the same object becomes the text of that element.

Why is my array not wrapped in a parent element?

An array under a key is expanded into repeated elements named after that key. If you need a wrapper such as <items><item>…</item></items>, build the JSON as {"items":{"item":[…]}}.

Will converting the XML back to JSON give me the same data?

Usually not. Numbers and booleans come back as strings, empty arrays are gone and a one-element array turns into a plain value. The round trip is lossy.

Is my JSON uploaded to a server?

No, the conversion runs in your browser. Testing with sample data rather than real customer records is still a good habit.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice