There was a time when XML was simply how the web exchanged data. SOAP APIs, RSS feeds, config files, Microsoft Office documents under the hood — all XML. Then JSON showed up, and within a few years it had taken over almost everywhere XML used to dominate. Understanding why helps explain a lot about how modern APIs are designed, and it also explains the specific situations where XML is still the right tool.
The same data in both formats
Here's a small user record in XML:
<user id="1">
<name>Alice</name>
<roles>
<role>admin</role>
<role>editor</role>
</roles>
</user>
And the same thing in JSON:
{
"user": {
"id": 1,
"name": "Alice",
"roles": ["admin", "editor"]
}
}
Already you can see the core difference: XML wraps every value in opening and closing tags
(<name>Alice</name>), while JSON uses a colon and lets punctuation do the work. JSON
is shorter and, for most developers, faster to read at a glance. XML has something JSON doesn't, though:
attributes (like id="1" above) as a distinct concept from child elements — a genuine
structural feature, not just a style choice.
Why JSON won for APIs
- It's native to JavaScript. JSON is literally a subset of JavaScript object syntax. As browser-based web apps exploded, having a data format your frontend language could parse with zero extra work was a huge practical advantage.
- Less to type, less to parse. No closing tags to match, no attribute-vs-element decisions to make. Smaller payloads over the network, and simpler code to read them.
- Arrays are natural. Representing a list in XML requires either repeated elements or a wrapper convention that varies from API to API. JSON has one obvious way to write a list: square brackets.
- REST replaced SOAP. SOAP (the API style that leaned heavily on XML) came with a lot of ceremony — formal contracts, envelopes, strict schemas. REST's simpler, resource-based style paired naturally with JSON's simpler syntax, and REST became the default way people built APIs.
Where XML still matters
XML didn't disappear, and dismissing it as "obsolete" undersells where it's still the right choice:
- Document formats. Microsoft Word (.docx), Excel (.xlsx), and PowerPoint (.pptx) files are all ZIP archives full of XML internally. SVG images are XML. These formats predate JSON's dominance and have too much invested infrastructure to switch.
- Enterprise and legacy systems. Banking, healthcare, and government systems built SOAP-based integrations over decades. Replacing them isn't a technical decision so much as an enormous, risky migration project, so a lot of that infrastructure still runs on XML today.
- Mixed content. XML handles text with embedded markup naturally — think of a paragraph of text with some words in bold, some as links. This is XML's original use case (it grew out of SGML, the ancestor of HTML), and it's still awkward to express the same thing in JSON.
- Strict, formal validation. XML Schema (XSD) has been a mature, heavily standardized validation language for over two decades, with tooling that some regulated industries still rely on specifically because of that maturity.
The features JSON deliberately left out
JSON's simplicity is a design choice, not an accident. A few things XML has that JSON intentionally doesn't:
- Attributes vs. elements. JSON has one way to attach a value to something: a key. XML lets you choose between an attribute (
<user id="1">) and a child element (<user><id>1</id></user>), which sounds flexible but in practice creates endless "should this be an attribute or an element?" debates with no universally right answer. - Namespaces. XML namespaces let you mix vocabularies from different sources in one document without naming collisions. Genuinely useful for some document formats, genuinely confusing for most everyday use.
- Comments. XML supports
<!-- comments -->. Standard JSON has no comment syntax at all.
Converting between them
The mechanical part of converting JSON to XML is usually straightforward: object keys become element tags,
and arrays become repeated sibling elements. The harder decision is what to do with JSON values that don't
map cleanly — should a JSON key become an XML attribute or a child element? There's no single correct
answer, only conventions, so pick one and apply it consistently. Converting XML to JSON has the reverse
problem: XML attributes need somewhere to go in a system that doesn't have a separate concept for them, which
is typically solved with a naming convention like prefixing attribute-derived keys with @.
The takeaway
If you're building a new API today, JSON is almost always the right default — the ecosystem, tooling, and developer expectations all point that way. But if you're integrating with an existing enterprise system, working with document formats, or need XML's formal validation and mixed-content handling, XML isn't a relic to be avoided on principle. It's a format with genuine strengths that happen to matter less for the kind of lightweight, JavaScript-adjacent APIs that make up most of the modern web.