From JSON to XML Without Tears
Online converters promise one-click JSON ↔ XML transformations, yet many fall short the moment your data contains arrays or irregular plurals. In this post I’ll show two common failure patterns, explain why they matter for Java object-mapping (As a senior Java engineer, I’ll illustrate the concept with Java-based example), and share a simple checklist for choosing a reliable tool.
Why most online converters break your POJOs
JSON is inherently schema-less, while XML thrives on structure. Good converters therefore need sensible rules for representing arrays and for turning plural names into singular element names. Unfortunately, many popular sites skip these subtleties and give you “flat” XML that cannot be deserialized back into your domain classes without manual patching.
Example ① — A Simple Country Object
Source JSON
{
"name": "Belgium",
"capital": "Brussels",
"population": 11589623,
"area": 30528,
"currency": "Euro",
"languages": [
"Flemish",
"French",
"German"
]
}
Typical output from JSONFormatter
<name>Belgium</name>
<capital>Brussels</capital>
<population>11589623</population>
<area>30528</area>
<currency>Euro</currency>
<languages>Flemish</languages>
<languages>French</languages>
<languages>German</languages>
The converter flattens the array into three identical <languages>
tags.
When you attempt to unmarshal this XML with JAXB, Jackson XML or XStream, the compiler will
complain because Java cannot have three fields named languages
.
The only workaround is to hand-edit the XML or write a custom adapter.
(JSONFormatter itself makes no claim to handle arrays in a structured way)
Desired structure
<country>
<name>Belgium</name>
<capital>Brussels</capital>
<population>11589623</population>
<area>30528</area>
<currency>Euro</currency>
<languages>
<language>Flemish</language>
<language>French</language>
<language>German</language>
</languages>
</country>
Corresponding Java model
public class Country {
private String name;
private String capital;
private int population;
private int area;
private String currency;
private List<Language> languages;
public static class Language {
private String value; // JAXB: @XmlValue
}
}
Example ② — Irregular Plurals
Source JSON
{
"name": "Kindergarten",
"children": [
"Alice",
"Hanna",
"Thomas"
]
}
Flat output (problematic)
<name>Kindergarten</name>
<children>Alice</children>
<children>Hanna</children>
<children>Thomas</children>
Again we get duplicate <children>
tags and no attempt to derive the
correct singular tag name (<child>
).
A smarter converter—such as JsonWizard—detects many
irregular plurals and emits this instead:
<kindergarten>
<name>Kindergarten</name>
<children>
<child>Alice</child>
<child>Hanna</child>
<child>Thomas</child>
</children>
</kindergarten>
A 3-step checklist for choosing a converter
- Inspect array handling. Paste a quick sample with at least two items. Look for a wrapper element and unique child names.
- Check plural heuristics.
Try both regular (
books → book
) and irregular (children → child
) nouns. - Round-trip test.
Convert JSON → XML → JSON. The final JSON should equal your input
(
equals()
deep comparison).
Final thoughts
Arrays and irregular plurals are the edge-cases that separate a toy converter from a production-ready one. Always validate the generated XML against your Java model (or XSD) before shipping test fixtures to CI. A few minutes of diligence here can save hours of “why won’t JAXB read my file?” debugging later.
Tested on May 29 2025 with the public versions of JSONFormatter and JsonWizard. Results may change as these services evolve.