JSON vs XML: What's the Difference?
Both carry structured data, but JSON won the web. Here's how they compare and why most APIs use JSON today.
JSON and XML are both text formats for structured data. JSON has become the default for web APIs; XML is still common in enterprise systems, document formats, RSS/Atom feeds, SOAP services and configuration. Many APIs can return either — you choose with an Accept header or a format= parameter.
The same data, both ways
// JSON
{ "book": { "title": "Dune", "year": 1965, "authors": ["Herbert"] } }
<!-- XML -->
<book>
<title>Dune</title>
<year>1965</year>
<authors><author>Herbert</author></authors>
</book>How they compare
| Verbosity | JSON is more compact — no closing tags. XML repeats every element name. |
| Data types | JSON has numbers, booleans and null natively. In XML everything is text; the schema (or your code) decides what <year>1965</year> means. |
| Parsing | JSON.parse / json.loads is one line and built in everywhere. XML needs a DOM or streaming parser and is fiddlier. |
| Attributes | XML distinguishes attributes (<book id="5">) from child elements. JSON has only keys and values, so mapping XML to JSON is sometimes ambiguous. |
| Comments | XML allows <!-- -->. JSON has none. |
| Namespaces & schema | XML has mature tooling: XSD validation, XSLT transforms, XPath queries, namespaces. JSON’s equivalents (JSON Schema, JSONPath) exist but are lighter. |
| Mixed content | XML can interleave text and markup (<p>hi <b>there</b></p>) — which is why documents use it. JSON cannot represent that naturally. |
When you will meet XML
- RSS and Atom feeds (news, podcasts, blogs).
- SOAP web services — older banking, telecom, government and enterprise APIs.
- Sitemaps and many configuration files (Maven, Android layouts).
- Office document formats (
.docx,.xlsxare zipped XML). - Some scientific and geospatial APIs (e.g. arXiv returns Atom XML).
Requesting the format you want
# ask for JSON curl -H "Accept: application/json" https://api.example.com/books/5 # ask for XML (if supported) curl -H "Accept: application/xml" https://api.example.com/books/5 # some APIs use a query parameter instead curl "https://api.example.com/books/5?format=xml"
Parsing XML when you have to
import xml.etree.ElementTree as ET
root = ET.fromstring(xml_text)
title = root.findtext(".//title")
authors = [a.text for a in root.iterfind(".//author")]For anything but trivial XML, use a well-tested library (lxml, xmltodict in Python; the browser’s DOMParser in JavaScript) rather than regular expressions.
FAQ
Which is better, JSON or XML?
For web APIs and app-to-app data, JSON — it is lighter and parses natively everywhere. For documents, feeds, and systems that need schema validation, transforms and namespaces, XML still earns its place.
Can I convert XML to JSON?
Yes — libraries like xmltodict do it — but the mapping is lossy: XML attributes, namespaces, comments and element order don’t all survive cleanly. Test with your real data.
Why do some APIs still return XML?
Legacy. Many enterprise and government systems were built on SOAP/XML and remain in production. Newer endpoints from the same providers often offer JSON alongside.
Is YAML related to these?
YAML is a third text format, popular for configuration because it is the least punctuation-heavy. It is a superset of JSON but rarely used as an API wire format.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ