JSON vs XML vs YAML: Differences With Examples
Published November 25, 2026 · 7 min read
JSON, XML and YAML all store structured data as text. JSON is the standard for web APIs, YAML is popular for config files because it is easy to read, and XML is still used for documents, feeds and older enterprise systems. The easiest way to see the difference is to look at the same data in all three.
Working with JSON right now? Format and validate it in the free JSON Formatter.
The same data, three ways
JSON
{
"user": {
"name": "Ayesha Khan",
"age": 28,
"active": true,
"skills": ["PHP", "Vue.js", "MySQL"]
}
}
XML
<user>
<name>Ayesha Khan</name>
<age>28</age>
<active>true</active>
<skills>
<skill>PHP</skill>
<skill>Vue.js</skill>
<skill>MySQL</skill>
</skills>
</user>
YAML
user:
name: Ayesha Khan
age: 28
active: true
skills:
- PHP
- Vue.js
- MySQL
Key differences
| JSON | XML | YAML | |
|---|---|---|---|
| Readability | Good | Wordy | Very easy |
| File size | Small | Largest (closing tags) | Smallest |
| Data types | String, number, boolean, null, array, object | Everything is text unless a schema says otherwise | Same as JSON, plus dates and more |
| Comments | Not allowed | Allowed | Allowed (#) |
| Whitespace matters | No | No | Yes, indentation is structure |
| Native in browsers | Yes (JSON.parse) | Yes (DOMParser) | No, needs a library |
| Typical use | REST APIs, web apps, NoSQL | SOAP, RSS, sitemaps, Office files, SVG | Docker Compose, Kubernetes, CI pipelines |
When to use JSON
Use JSON for anything sent between a browser and a server. Every language has a fast built-in parser,
and JavaScript reads it natively. Frameworks make it a one-liner: Laravel turns arrays and models into
JSON automatically, Flask does the same for dictionaries, and Django has JsonResponse. To
find your way around a real response, see
How to Read a JSON API Response.
When to use XML
Use XML when a system requires it: SOAP web services, RSS feeds, sitemaps, invoices in some government and banking systems, and document formats. XML also supports attributes, namespaces and strict schema validation (XSD), which some industries rely on.
When to use YAML
Use YAML for files that people edit by hand, such as docker-compose.yml, GitHub Actions
workflows and Kubernetes manifests. Comments and the clean layout make it easy to maintain. Be careful
with indentation: one wrong space changes the meaning or breaks the file. Sharing one for help? Follow
the config file redaction
checklist first, because YAML files often hold passwords and keys.
A note on YAML surprises
YAML guesses data types. In older YAML 1.1 parsers (still common, such as PyYAML), values like
no, yes, on and off become booleans, so a country
code NO (Norway) can turn into false. Quote strings that could be misread:
country: "NO". The same goes for version numbers: version: 1.10 becomes the
number 1.1.
Converting between them
- JSON to YAML: YAML 1.2 is a superset of JSON, so valid JSON is usually valid YAML too. Converters mostly just change the layout.
- XML to JSON: attributes and repeated tags need rules (is one
<skill>a string or a one-item list?), so check the output by hand. - Before converting, make sure your JSON is valid with the JSON Formatter. If it isn't, Unexpected Token in JSON lists the usual causes, and How to Format, Validate and Fix JSON covers the rest.
Paste JSON to indent it, minify it or check it for errors, with the line and column of any mistake.
Frequently asked questions
Is JSON better than XML?
For web APIs, yes, because it is smaller and faster to parse. XML is better when you need schemas, attributes or document markup.
Why is YAML used for config files?
It is easy to read and write by hand, and it supports comments, which JSON does not.
Can JSON have comments?
No. Standard JSON has no comment syntax. Use YAML, or a separate description field.
Is YAML a superset of JSON?
YAML 1.2 is designed so that almost all valid JSON is also valid YAML.
Related articles
"Unexpected Token in JSON": What It Means and How to Fix It
What JSON.parse errors mean in Chrome, Firefox, Python and PHP, the 7 most common causes from trailing commas to HTML responses, and how to find the exact line.
Read More →How to Read a JSON API Response (Beginner Guide)
Format a one-line response, learn the six value types, and follow paths like data.orders[0].total in JavaScript, Python and PHP.
Read More →How to Share a Config File Safely: A Redaction Checklist for .env, YAML and nginx
Share .env, YAML, JSON and nginx configs for debugging without leaking secrets: what to redact, consistent placeholders, examples, and what to do if a key was exposed.
Read More →