JSON vs YAML vs XML: which one should you use?
Your API returns JSON, your Docker Compose and CI files are YAML, and the bank or government system you have to integrate with still speaks XML. All three formats store the same kind of thing — structured data — but they were designed for different jobs, and each has traps that only show up in production. This guide puts the same data side by side, compares them honestly, and gives you a simple rule for choosing.
The same data in three formats
Here is a small service configuration: a name, a port, a flag, a country code, a version, a list of admins and a nested database block.
JSON
{
"service": "checkout",
"port": 8080,
"debug": false,
"country": "NO",
"version": "3.10",
"admins": ["rahim@example.com", "karim@example.com"],
"database": { "host": "db.internal", "pool": 10 }
}
YAML
# Checkout service settings
service: checkout
port: 8080
debug: false
country: "NO" # quoted on purpose, see below
version: "3.10"
admins:
- rahim@example.com
- karim@example.com
database:
host: db.internal
pool: 10
XML
<?xml version="1.0" encoding="UTF-8"?>
<!-- Checkout service settings -->
<service name="checkout">
<port>8080</port>
<debug>false</debug>
<country>NO</country>
<version>3.10</version>
<admins>
<admin>rahim@example.com</admin>
<admin>karim@example.com</admin>
</admins>
<database host="db.internal" pool="10"/>
</service>
Three things stand out. YAML is the shortest and allows comments. JSON is compact and unambiguous. XML is the most verbose, but it has two ways to hold data — child elements and attributes (name="checkout") — and in XML every value is just text: 8080 and false are strings unless a schema says otherwise.
Side-by-side comparison
| JSON | YAML | XML | |
|---|---|---|---|
| Readability | Good; noisy with many quotes and braces | Best for humans when short; hard to follow when deeply nested | Verbose; closing tags double the size |
| Comments | No | Yes (#) | Yes (<!-- -->) |
| Data types | string, number, boolean, null, object, array | Same plus more; types are guessed from the text unless quoted or tagged | Everything is text; types come from a schema |
| Schema / validation | JSON Schema | Usually JSON Schema (YAML maps onto the same data model) | XSD, DTD, RELAX NG — the most mature |
| Strictness | Very strict, tiny grammar | Large, complex spec; parsers differ | Strict, well-formedness is checked |
| Tooling | Built into every language and browser | Needs a library in most languages | Mature libraries, XPath, XSLT |
| Typical uses | REST APIs, web apps, package.json, NoSQL documents | Kubernetes, Docker Compose, GitHub Actions, OpenAPI specs | SOAP services, RSS/Atom feeds, SVG, Office files, Android layouts, Maven |
JSON: the default for data exchange
JSON’s strength is that it is boring. There is exactly one way to write a string, a number or a boolean, so two programs in different languages always agree on what the data means. JSON.parse is built into every browser, and parsing is fast. The price is that it is unpleasant to hand-edit: no comments, no trailing commas, and every key needs quotes. That is why it is ideal between programs and less ideal for files people maintain by hand.
YAML: friendly, but with sharp edges
YAML was designed for humans: indentation instead of brackets, comments, multi-line strings, and anchors (&/*) to reuse blocks. YAML 1.2 is (almost) a superset of JSON, so most valid JSON is also valid YAML. But the friendliness comes from guessing, and guessing goes wrong:
The Norway problem. In YAML 1.1, unquoted yes, no, on, off (in several capitalisations) are booleans. So country: NO becomes country: false. YAML 1.2 fixed this, but popular parsers such as Python’s PyYAML still follow 1.1. Quote any value that could be mistaken for something else.
- Numbers that aren’t numbers.
version: 3.10is read as the float3.1. Version strings, postcodes and phone numbers need quotes. - Indentation is the structure. Tabs are not allowed for indentation, and moving a line two spaces left silently moves it to a different parent. A diff that looks like a whitespace change can change meaning.
- Many ways to say the same thing. Strings can be plain, single-quoted, double-quoted,
|literal or>folded, each with its own escaping and newline rules. - Unsafe loading. Some YAML loaders can create arbitrary objects from tags. In Python always use
yaml.safe_load()for untrusted input.
XML: verbose, but built for documents
XML predates JSON and was designed for marked-up documents as much as for data. It supports mixed content (text with tags inside it, like HTML), namespaces for combining vocabularies, and the most powerful schema and transformation tools of the three: XSD for validation, XPath for querying and XSLT for transforming. That is why formats such as SVG, RSS, DOCX/XLSX internals and many financial and government interfaces are XML. The costs are size, verbosity and a design choice with no single right answer — element or attribute? For new web APIs, XML is rarely chosen today; for integrating with systems that already use it, you simply follow their schema.
Security note: an XML parser that resolves external entities can be tricked into reading local files (an “XXE” attack). When parsing XML from outside, make sure DTD and external entity processing is disabled in your library.
How to choose
- Programs talking to programs (APIs, messages, storage): JSON.
- Config files that people edit and review: YAML if the ecosystem expects it (Kubernetes, CI); otherwise JSON, or a JSON-with-comments variant your tool supports. Keep YAML flat and quote anything ambiguous.
- Documents, or a partner who requires it: XML, validated against their XSD.
You don’t have to commit forever. Because all three describe trees of values, converting between them is mechanical — just check that nothing was re-typed along the way.
"NO" are quoted for you.
Try it now
FAQ
Is YAML a superset of JSON?
Since YAML 1.2, practically yes: a JSON document can be loaded by a YAML 1.2 parser. The reverse is not true — comments, anchors and unquoted strings are not JSON.
Is JSON faster than XML?
Usually JSON is smaller on the wire and quicker to parse, especially in browsers where the parser is built in. For most apps the difference matters far less than network time, so choose by fit, not speed.
Why did my YAML value turn into true or a different number?
YAML guesses types for unquoted values. Words like no or on can become booleans in YAML 1.1 parsers, and 3.10 becomes 3.1. Put such values in quotes.
Can I validate YAML with JSON Schema?
Yes, this is common. Load the YAML into data, then validate that data with a JSON Schema validator; many editors do this for Kubernetes and CI files.