DEV Community

Mubeen Khan
Mubeen Khan

Posted on Edited on

Your XML has three books. Your JSON has one. The converter chose wrong.

I pasted a small catalog into an XML to JSON converter last week. Three <book> elements went in. One book came out. No error, no warning, just a quiet data loss dressed up as a successful conversion.

The converter was not broken. It was doing exactly what most naive converters do: it turned each element name into an object key, and when the same key appeared three times, the last one won. The first two books were overwritten before I ever saw the output. The tool had no way to know I wanted an array, because nobody told it that repeated siblings are a list.

That is the real lesson of XML to JSON conversion. It is not a format change. It is a translation between two languages that describe data differently, and every converter has to pick a convention for the parts that do not map one to one. The converter you use matters less than knowing which convention it picked.

Attributes have no home in JSON

XML has two ways to attach information to an element: child elements and attributes. JSON has only one: keys in an object. So a converter has to decide where attributes go, and there is no single right answer.

Take this element:

<book id="bk-204" lang="en">
  <title>The Art of Simplicity</title>
</book>
Enter fullscreen mode Exit fullscreen mode

One converter writes the attribute as a key with an @ prefix, sitting next to the child elements:

{
  "book": {
    "@id": "bk-204",
    "@lang": "en",
    "title": "The Art of Simplicity"
  }
}
Enter fullscreen mode Exit fullscreen mode

Another groups them under a single @attributes object. A third drops them entirely, which is the silent killer: your id values vanish and the JSON still looks perfectly valid. If the API you are feeding expects the attributes, a converter that drops them has not converted your data, it has edited it.

Before you trust any converter, paste an element with an attribute and look at where it lands. If you cannot find it, stop using that tool.

Text needs a key too

The same problem shows up with text content. An element that has both attributes and text has nowhere natural to put the text in JSON, because JSON objects hold key-value pairs and a bare string is not a pair. Converters solve this with a reserved key, usually #text:

{
  "price": {
    "@currency": "USD",
    "#text": "29.99"
  }
}
Enter fullscreen mode Exit fullscreen mode

A text-only element is simpler: it just becomes a string. <title>The Art of Simplicity</title> becomes "title": "The Art of Simplicity", no wrapper needed. The rule is consistent once you know it, but the first time you see #text in output it looks like a bug. It is not. It is the converter being honest about a real structural difference.

The three checks I run on any converter

I test a converter with the same small document before I trust it with a real feed:

  1. Repeated elements. Three <book> elements must come out as an array of three objects. If you get one object, the converter overwrites duplicates and will eat your data.
  2. Attributes. An id attribute must appear in the output, with a documented prefix. If it is missing, the converter is lossy.
  3. Malformed input. Paste XML with an unclosed tag. A good converter reports the exact line and column. One that guesses or outputs partial JSON is more dangerous than one that refuses, because bad output that looks valid gets shipped.

Two more details worth knowing. CDATA sections are text, so a converter should unwrap them into the string, not choke on the brackets. And a DOCTYPE declaration should be refused rather than processed: it is the entry point for XXE attacks, where a crafted external entity makes the parser read local files. A converter that silently accepts a DOCTYPE is not being flexible, it is being careless.

I built the version I wanted to use into The Utility Hub's free XML to JSON Converter. It shows you the convention up front: attributes under @, text under #text, repeated elements as arrays, an unwrap-root toggle, and optional type inference so 29.99 can become a real number instead of staying a string. It validates first and reports the exact line and column when the XML is malformed, and it refuses DOCTYPE outright. Everything runs in your browser, so the XML never leaves your machine, which matters when the feed you are converting is a customer export or an internal config.

The rule before you paste the result into your code

Read the output once, with the input beside it, and count. Count the books. Count the attributes. If the numbers match, the convention is sound and you can trust the tool with the 10,000-line file. If the count is off by even one, the converter made a choice for you, and it chose wrong. Pick a tool that shows its choices instead of hiding them.

Top comments (0)