JSON, XML, and YAML are three of the most common ways to represent structured data as plain text. They look different, but they all do the same core job: take nested data like objects, lists, numbers, and strings, and turn it into something a human can read and a machine can parse. The hard part is not learning the syntax of each one, it is knowing which to reach for when you start a new API, write a config file, or pick a storage format.
This guide compares json vs xml vs yaml head to head on the things that actually matter day to day: readability, file size, comment support, tooling, and the use cases where each one quietly wins. By the end you will have a clear rule of thumb instead of a vague gut feeling.
The same data, three ways
The fastest way to feel the difference is to express one small record in all three formats. Here is a user with an id, a name, and a list of roles.
JSON
JSON (JavaScript Object Notation) uses braces for objects, square brackets for arrays, and double-quoted keys.
{ 'id': 42, 'name': 'Ada Lovelace', 'roles': ['admin', 'author'] }
(In real JSON those single quotes are double quotes. If you want a refresher on the exact rules, see what is JSON and the JSON syntax rules.)
XML
XML (eXtensible Markup Language) wraps every value in an opening and closing tag, and can also attach attributes to tags.
<user id="42"><name>Ada Lovelace</name><roles><role>admin</role><role>author</role></roles></user>
YAML
YAML (YAML Ain't Markup Language) drops most punctuation and uses indentation plus dashes for lists, which makes it feel almost like an outline.
id: 42
name: Ada Lovelace
roles:
- admin
- author
Notice how the same information gets progressively shorter and more human-friendly as you move from XML to JSON to YAML, while XML carries the most explicit structure. That trade-off, verbosity versus brevity, runs through this entire comparison.
Quick comparison table
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Primary use | APIs, data exchange | Documents, enterprise, SOAP | Config files |
| Readability | Good | Verbose | Excellent |
| File size | Compact | Largest | Compact |
| Comments | No (by spec) | Yes | Yes |
| Data types | Built-in (string, number, boolean, null) | Everything is text | Built-in, inferred |
| Attributes / metadata | No | Yes | No |
| Schema / validation | JSON Schema | XSD, DTD | Uses JSON Schema tooling |
| Whitespace sensitive | No | No | Yes (indentation matters) |
Readability and verbosity
XML is the most explicit and the most verbose. Every field is named twice, once in the opening tag and once in the closing tag, which makes long documents repetitive and noisy. That verbosity is not purely a flaw, though: tags are self-describing, and attributes let you attach metadata (like the id="42" above) without inventing extra fields.
JSON strikes a middle ground. It is compact, the structure is obvious from the braces and brackets, and almost every programming language can parse it without extra libraries. The main readability complaint is that deeply nested JSON drowns in punctuation, and a single missing comma or bracket breaks the whole document.
YAML is usually the easiest for a human to scan because it removes braces, brackets, and most quotes, relying on indentation instead. That is great for a config file you edit by hand. The catch is that YAML is whitespace-sensitive: mixing tabs and spaces, or misaligning a single line, silently changes the meaning of your data or throws a parse error. What you gain in reading comfort, you can lose in editing safety.
File size and performance
For the same data, XML is almost always the largest because of its repeated closing tags. JSON and YAML are comparable in size and both noticeably smaller than XML. When you are sending thousands of records over a network, that difference adds up, which is one reason JSON overtook XML as the default for web APIs.
On parsing speed, JSON tends to be the fastest in practice because parsers are mature, heavily optimized, and built into most runtimes. YAML parsing is generally slower because the format is richer and more ambiguous, so it does more work to figure out what a value means. XML parsing sits in between, and varies a lot by library. None of these differences matter for a small config file, but they do matter for high-throughput APIs.
Comments: a real deciding factor
This is one of the most practical differences, and it surprises a lot of developers.
JSON does not support comments. The official spec has no comment syntax, so you cannot leave a note like // explain this setting in a strict JSON file.XML and YAML both support comments. In XML you use <!-- like this -->, and in YAML you use a # at the start of a line. For a configuration file that humans maintain, the ability to annotate why a value exists is genuinely valuable, and it is a big reason tools like Docker Compose, Kubernetes, and GitHub Actions chose YAML.
If you need comments but are stuck with JSON, you have a few workarounds: add a throwaway key such as { '_comment': 'explanation here' }, or use a relaxed superset like JSON5 or JSONC for config (just remember that strict parsers and our free JSON formatter and validator will reject real comments in standard JSON).
Data types and structure
JSON has a small, well-defined set of types built in: strings, numbers, booleans, null, arrays, and objects. A parser knows that 42 is a number and true is a boolean without any guessing. That predictability is a major strength for data exchange between systems written in different languages.
In XML, everything between tags is text. A number, a date, or a boolean all arrive as strings, and it is up to your code (or an attached schema) to interpret them. XML makes up for this with rich structural features like namespaces and attributes, which is why it endures in document-heavy and enterprise messaging contexts.
YAML infers types similarly to JSON, but its flexibility can bite you. The classic example is the so-called Norway problem: an unquoted NO (a country code) can be interpreted as the boolean false. When precision matters in YAML, quote your strings. Because YAML is a superset of JSON, any valid JSON is also valid YAML, which makes converting between the two straightforward.
Validation and schemas
If you need to guarantee that data has the right shape, all three have an answer. XML has long-established options in XSD and DTD. JSON uses JSON Schema, which describes required fields, types, and constraints, and has excellent tooling. YAML typically borrows JSON Schema tooling because the two formats map cleanly onto each other. So validation is not a reason to pick one over another, it is available everywhere, but the JSON Schema ecosystem is the most active today.
When to use each format
Use JSON when...
- You are building or consuming a web API or sending data between services.
- You want broad, native language support and fast parsing.
- The data is produced and read by machines more than hand-edited by people.
Use XML when...
- You are working with legacy or enterprise systems, SOAP services, or document formats like RSS, SVG, or office files.
- You need attributes, namespaces, or mixed content (text interleaved with markup).
- A standard in your industry already mandates it.
When to use YAML
- You are writing configuration files that humans edit by hand.
- You want inline comments to explain settings.
- You are in the DevOps world, Kubernetes, Docker Compose, Ansible, and CI pipelines, where YAML is the de facto standard.
A simple way to remember it: JSON for talking between programs, YAML for talking to humans, XML for documents and legacy systems.
Common pitfalls to watch for
Each format has a signature failure mode. With JSON, it is usually a trailing comma, a missing bracket, or single quotes where double quotes belong, the kinds of issues covered in common JSON mistakes and in our guide to fixing parse errors. With YAML, it is almost always indentation and unquoted values changing meaning. With XML, it is usually an unclosed tag or an unescaped special character like &.
Whichever format you use, run it through a validator before you ship it. If you are working in JSON, you may also want to decide between a compact and a readable layout, which we cover in minify vs prettify JSON.
Key takeaways
- JSON is the default for APIs and data exchange: compact, fast, universally supported, but no comments.
- XML is the most verbose but the most expressive, with attributes, namespaces, and mature schemas, ideal for documents and legacy enterprise systems.
- YAML is the most human-readable and supports comments, which makes it the go-to for config files, but its indentation sensitivity and type inference demand care.
- For json vs yaml specifically: same data model, so choose JSON for machine-to-machine traffic and YAML for human-edited config.
- For json vs xml: prefer JSON unless a standard, document format, or legacy system requires XML.
- Validate before you ship, no matter which one you pick.