JSON vs XML: Differences, Examples and When Each One Still Makes Sense
JSON won the web API wars, but XML is far from dead. It still runs invoices, sitemaps, RSS feeds, office documents and plenty of enterprise systems. Here's how the two compare, with examples, and when each makes sense.

On this page
If you started programming in the last ten years, you probably think of JSON as simply how data moves around. Ask for a user from an API and you get curly braces. But open a sitemap, an RSS feed, an Android layout or the inside of a .docx file, and you'll find angle brackets everywhere. XML didn't go away; it just stopped being the default.
Here's json vs xml compared side by side, with examples, and a practical take on when each one is the right choice today.
The same data, both ways#
An order in JSON:
{
"id": 1042,
"customer": { "name": "Ada Lovelace", "email": "ada@example.com" },
"paid": true,
"items": [
{ "sku": "MUG-01", "qty": 2, "price": 9.5 },
{ "sku": "TEE-03", "qty": 1, "price": 18 }
]
}
The same order in XML:
<order id="1042">
<customer>
<name>Ada Lovelace</name>
<email>ada@example.com</email>
</customer>
<paid>true</paid>
<items>
<item sku="MUG-01" qty="2" price="9.5"/>
<item sku="TEE-03" qty="1" price="18"/>
</items>
</order>
Both are readable. JSON is shorter. XML has a choice JSON doesn't: put a value in an attribute (id="1042") or in an element (<id>1042</id>). That flexibility is useful and also a source of endless debate.
The key differences#
Data types#
JSON has built-in types: strings, numbers, booleans, null, arrays and objects. When you parse it, "paid": true becomes a real boolean. In XML, everything is text unless a schema says otherwise. "true" is just four characters until your code decides what it means.
Arrays#
JSON has arrays. XML has repeated elements, and a common bug is code that expects a list but gets a single item when there's only one <item>. Many XML-to-JSON converters have exactly this problem.
Size and speed#
XML repeats every tag name twice, opening and closing, so the same data is usually larger. Compression narrows the gap a lot, and for most apps the difference doesn't matter. JSON parsers are typically simpler and fast.
Comments#
XML supports comments. Standard JSON doesn't, which is why config formats built on JSON often add their own variants.
Schemas and validation#
XML has mature schema languages (XSD, and others) that many industries rely on to define exactly what a valid document looks like, including types, required fields and allowed values. JSON has JSON Schema, which is widely used and capable, but the XML tooling is older and deeply embedded in certain sectors.
Namespaces#
XML namespaces let documents mix vocabularies without name clashes, which matters in large standards. JSON has no equivalent; you rely on conventions.
Mixed content#
XML handles text with markup inside it naturally: <p>Hello <b>world</b></p>. That's why document formats use it. JSON is awkward for this.
Where XML is still everywhere#
- Sitemaps and RSS or Atom feeds, which search engines and feed readers expect.
- SOAP web services, still common in banking, insurance, government and logistics.
- E-invoicing and financial messaging standards in many countries and industries.
- Office files: .docx, .xlsx and .pptx are zip files full of XML.
- SVG images, which are XML you can open in a text editor.
- Android layouts and manifests, and many build tool configs such as Maven's pom.xml.
If you integrate with any of these, you'll work with XML whatever your preference.
Where JSON dominates#
- REST APIs for web and mobile apps. Our REST API guide for beginners uses JSON throughout.
- Webhooks, like payment, SMS and WhatsApp notifications.
- Browser apps, because JSON maps directly onto JavaScript objects.
- NoSQL databases and many logging systems.
- Config files for JavaScript tooling.
Parsing each one in practice#
In most languages, JSON is a one-liner: JSON.parse in JavaScript, json.loads in Python, json_decode in PHP. You get native objects or arrays back and use them straight away.
XML takes a little more thought, because you choose a parsing style. DOM parsers load the whole document into a tree you can navigate, which is simple for small files. Streaming parsers (SAX, or pull parsers like PHP's XMLReader) read the document piece by piece, which is the only sensible option for very large files like product feeds with hundreds of thousands of items. XPath lets you query a tree directly, such as //item[@sku='MUG-01'], and is genuinely handy once you know it.
A practical tip for either format: write a tiny test with a real sample from the system you're integrating with, not a hand-made example. Real data has the empty fields, odd characters and single-item lists that hand-made samples forget, and it's those cases that break integrations in production.
Security notes#
Both formats need care when parsing untrusted input. XML has a specific class of attacks around external entities (XXE), where a crafted document makes the parser read local files or make network requests. Modern libraries often disable this by default, but check your parser's settings and turn off external entity resolution. For JSON, the main risks are huge or deeply nested payloads, so set size limits, and never use eval() to parse it.
Converting between them#
Libraries can convert XML to JSON and back, but there's no single correct mapping. Attributes might become "@id" keys or plain fields; single items might become objects or one-item arrays; text inside mixed content may be dropped. Decide on the mapping, test with real documents, and check what the receiving system expects.
How to choose#
- Building a new API for web or mobile clients: JSON.
- Integrating with an existing standard or partner that uses XML: XML, and don't fight it.
- Documents with formatting and markup: XML (or an XML-based format).
- Config files that humans edit: JSON, YAML or TOML, depending on your ecosystem; XML if the tool requires it.
- Strict, industry-wide validation: often XML with XSD, though JSON Schema is increasingly used.
Debugging tips#
Most "the API is broken" moments come down to malformed data: a trailing comma in JSON, an unescaped ampersand in XML, a missing closing tag, smart quotes pasted from a document. Paste the payload into a validator and it'll point to the exact line. The Tekservix JSON formatter pretty-prints and validates JSON in your browser, and the developer tools include an API tester for checking what an endpoint really returns. Browsers display XML files as a tree too, which is a quick way to see whether a document is well-formed.
In the end it's rarely a question of which format is better. It's which one the systems you connect to expect. Learn to read both comfortably, and use JSON when you get to choose.
Frequently asked questions
Is JSON better than XML?
For most web and mobile APIs, JSON is simpler, smaller and maps directly to data structures in modern languages. XML is better when you need mixed text and markup, attributes, namespaces or strict schemas that existing industry standards already define.
Is XML still used?
Yes. Sitemaps, RSS and Atom feeds, SOAP web services, many banking and e-invoicing standards, Android layouts, SVG images and the files inside Word and Excel documents are all XML.
Can I convert XML to JSON?
Yes, and many libraries do it, but there's no single correct mapping. Attributes, repeated elements and mixed content can be represented in several ways, so check the output against what the receiving system expects.



