Before JSON took over, XML was the universal format for exchanging data between systems. Both formats are still in active use today, and understanding their trade-offs helps you read legacy systems and make good design decisions. In this lesson of our JSON tutorial series, we compare the two formats side by side and explain when each one is the right choice.
Here is a small product record in JSON:
{
"product": {
"id": 501,
"name": "Mechanical Keyboard",
"price": 89.99,
"tags": ["input", "gaming"]
}
}And the equivalent XML:
<product>
<id>501</id>
<name>Mechanical Keyboard</name>
<price>89.99</price>
<tags>
<tag>input</tag>
<tag>gaming</tag>
</tags>
</product>The XML version repeats every element name as a closing tag, which makes it noticeably longer. For large payloads this verbosity translates into real bandwidth and parsing cost.
price could live in an element, an attribute, or text content, and consumers must know which.<price>89.99</price> is just a string.XML is not obsolete. It retains genuine strengths:
For structured data exchange between programs, JSON is smaller, easier to read, faster to parse, and maps cleanly onto native data structures. That is why essentially all new public web APIs are JSON-first, why configuration formats such as package.json chose it, and why document databases adopted it as their storage model.
Choose JSON for APIs, configuration, logging, and anything consumed by JavaScript. Choose XML when you are integrating with an existing XML ecosystem such as SOAP or RSS, or when your data is a marked-up document rather than a record. Many enterprises run both, exposing modern JSON APIs in front of older XML services.
Next up: Parsing JSON in JavaScript, where JSON.parse and JSON.stringify get a full, practical treatment.