If you build websites, you meet both formats: JSON in APIs and configuration files, XML in sitemaps, RSS feeds and SVG images. They describe structured data in different ways. Our dom4j guides cover working with XML in Java; this page compares the two formats.
The same data in both formats
<book id="b1" lang="en">
<title>Web hosting explained</title>
<price currency="EUR">19.95</price>
<tags><tag>hosting</tag><tag>beginners</tag></tags>
</book>{
"id": "b1",
"lang": "en",
"title": "Web hosting explained",
"price": { "amount": 19.95, "currency": "EUR" },
"tags": ["hosting", "beginners"]
}Key differences
| JSON | XML | |
|---|---|---|
| Syntax | Compact, objects and arrays | Tags, more verbose |
| Data types | Strings, numbers, booleans, null, arrays, objects | Everything is text unless a schema says otherwise |
| Attributes | No (just properties) | Yes |
| Namespaces | No | Yes |
| Mixed content (text with markup) | Awkward | Natural |
| Schemas | JSON Schema | XSD, DTD, RELAX NG |
| Querying | JSONPath, code | XPath, XSLT |
| Comments | Not allowed | Allowed |
When to choose JSON
- Web APIs and JavaScript applications.
- Configuration files for modern tools.
- Structured data on web pages (JSON-LD, see structured data).
When to choose XML
- Standards that require it: sitemaps, RSS/Atom, SVG, e-invoicing formats.
- Documents with mixed text and markup.
- Strict validation and long-term archiving.
- Integrations with enterprise systems.
Frequently asked questions
Is XML outdated?
No, it is just less fashionable for web APIs. XML remains the standard for sitemaps, feeds, SVG, office file formats, e-invoicing and many industry data exchanges.
Can I convert XML to JSON?
Yes, but not always one-to-one: attributes, namespaces and mixed text content have no direct JSON equivalent, so you must decide how to map them.