DEV Community

Mubeen Khan
Mubeen Khan

Posted on

JSON and XML do not describe data the same way, and every converter has to pick a side

Every API speaks JSON now. Every legacy system still speaks XML. Sooner or later you have to move data from one to the other, and that is when you discover the two formats do not describe data the same way at all.

JSON has six kinds of value: objects, arrays, strings, numbers, booleans and null. XML has elements, attributes and text. There is no one-to-one mapping between them, so every converter has to make four decisions for you, and most of them make those decisions silently.

Decision 1: what is the root?

XML demands exactly one root element. JSON does not. Your JSON might be an object, an array, or even a single string, and the converter has to wrap it in something.

{ "name": "Ana", "city": "Karachi" }
Enter fullscreen mode Exit fullscreen mode

becomes:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name>Ana</name>
  <city>Karachi</city>
</root>
Enter fullscreen mode Exit fullscreen mode

That <root> is a choice, not a fact. Some converters name it after your file. Some let you pick the name. If the system receiving your XML expects <order> or <catalog>, a converter that hardcodes <root> hands you a file you have to edit by hand. Pick one that lets you name the root before you convert, not after.

Decision 2: what happens to arrays?

XML has no array type. JSON does. So when the converter meets this:

{ "tags": ["new", "sale", "featured"] }
Enter fullscreen mode Exit fullscreen mode

it has to choose. The honest choice is to repeat the element:

<tags>
  <item>new</item>
  <item>sale</item>
  <item>featured</item>
</tags>
Enter fullscreen mode Exit fullscreen mode

The lazy choice is to join the values into one string, new,sale,featured, which destroys the structure and makes the data unparseable on the other side. If you are converting a list of products, orders or line items, repeated elements are the only representation that survives a round trip. Check what your converter does with an array before you trust it with a real payload.

Decision 3: attributes or elements?

XML has attributes. JSON does not. So a converter has to decide whether {"id": 42} becomes <product><id>42</id></product> or <product id="42"></product>. Both are valid XML. They mean different things to the system reading them.

The convention most tools use is a prefix: keys starting with @ become attributes, and a special #text key holds the text content of an element. It is not a standard, it is a handshake, but it is the handshake almost every converter understands:

{
  "product": {
    "@id": "42",
    "@currency": "USD",
    "#text": "Running shoes"
  }
}
Enter fullscreen mode Exit fullscreen mode
<product id="42" currency="USD">Running shoes</product>
Enter fullscreen mode Exit fullscreen mode

If your converter does not support that convention, you cannot express attributes at all, and every piece of metadata becomes a child element whether the receiving system wants it there or not.

Decision 4: what about null?

JSON has null. XML has no equivalent. A converter meeting "discount": null has three options: emit an empty element <discount/>, skip the key entirely, or mark it explicitly with xsi:nil="true". Each one means something different. An empty element can mean "present but blank". Omitting the key can mean "not applicable". Only xsi:nil says, unambiguously, "this value is null". For a SOAP service or a config file, the wrong choice here is a silent bug: the field looks converted, but the receiving system reads it as something else entirely.

The keys that are not legal XML

There is one more trap, and it is the one that breaks conversions most often. JSON keys can be almost anything. XML element names cannot start with a digit, cannot contain spaces, and cannot contain most punctuation. A key like "2fa_enabled" or "first name" is perfectly good JSON and invalid XML.

A good converter repairs those names and tells you it did so, turning 2fa_enabled into something like _2fa_enabled and flagging the change. A bad converter either crashes on the first bad key or emits XML that no parser will accept, and you only find out when the other system rejects the whole file.

Why this still matters in 2026

Nobody writes new systems in XML. But RSS feeds, sitemaps, SOAP APIs, banking integrations, government portals, old ERPs and configuration files all still run on it. The JSON-to-XML bridge is not a legacy curiosity, it is a weekly task for developers who integrate with anything older than five years.

The conversion itself is simple once you know the four decisions: name your root, repeat elements for arrays, use the @ prefix for attributes, and decide what null means before you convert. Get those four right and the output is XML any parser will accept.

I built a free JSON to XML converter that makes all four choices visible instead of hiding them: you name the root and array items, toggle the XML declaration and indentation, pick how nulls are handled, and it flags every key it had to repair. It runs entirely in your browser, so nothing you paste is uploaded, and you can try it here: JSON to XML Converter.

The conversion is not the hard part. Knowing what the converter decided for you is.

Top comments (0)