JSON vs YAML: What's the Difference and When to Use Each?
Published
JSON and YAML are two notations for the same kinds of data. JSON marks structure with braces and quotes, which makes it easy for machines to read and write, so it dominates APIs and data exchange. YAML marks structure with indentation and allows comments, so it is popular for configuration files that people edit by hand. YAML 1.2 can read almost any JSON as is.
Open a web API response and you will see JSON, full of braces and quotes. Open a Docker Compose, Kubernetes or GitHub Actions config and you will see YAML, lined up by indentation. Both are ways of writing data as text, so why use two? This post puts the same data side by side in both formats, walks through the syntax differences, and covers when to pick which.
The same data, two notations
Here is a small piece of data with a name, a version, a list of tags and server settings. First, as JSON:
{
"name": "scottools",
"version": 2,
"tags": ["blog", "tools"],
"server": {
"port": 8080,
"https": true
},
"owner": null
}
Converted to YAML with the YAML to JSON Converter, it looks like this:
name: scottools
version: 2
tags:
- blog
- tools
server:
port: 8080
https: true
owner: null
The values are identical. JSON wraps objects in braces and lists in brackets, and puts every key and string in double quotes. YAML shows structure with indentation instead of brackets, writes list items with a leading hyphen, and needs no quotes for most strings. In this site's converter, the JSON with one item per line takes 13 lines, the YAML 9.
Where the syntax differs
The biggest difference is comments. JSON has no comment syntax. Douglas Crockford, who popularized JSON, has said he left them out on purpose, to stop people from using comments as parsing directives. That makes it hard to leave a note in a config file explaining why a value is set the way it is. YAML treats everything after # as a comment, so each setting can carry an explanation.
The second difference is strictness. JSON's rules are short and firm: strings take double quotes only, no trailing comma after the last item, and just six kinds of value (string, number, boolean, null, object, array). That simplicity is why the JSON Formatter can point to an error's exact line and column. YAML guesses whether an unquoted value is a number, a boolean or a string, and offers many more features: multi-line strings, anchors (&) and aliases (*) to reuse content, and several documents in one file. That convenience leaves more room for surprising interpretations. Another rule that often trips people up: YAML indentation must use spaces, never tabs.
YAML nearly contains JSON
YAML 1.2, released in 2009, made compatibility with JSON a major goal, and as a result almost every JSON document is also a valid YAML document. Feed the JSON above to a YAML 1.2 parser and you get the same data. That is also why a YAML file may write a short list in JSON style, like [blog, tools].
The reverse does not hold. YAML with comments, anchors or unquoted strings cannot be read by a JSON parser, so it has to go through a converter, and a few things need care. In YAML, version: 1.10 becomes the number 1.1, and zip: 01234 loses its leading zero and becomes 1234. Older YAML 1.1 parsers read yes and no as booleans, so the country code NO for Norway could turn into false. The YAML to JSON Converter tool description covers these pitfalls in detail.
Which one to use
For data passed between programs, JSON is the default. Web browsers and most languages read and write it out of the box, and its simple rules make parsing fast and predictable. It is standardized as ECMA-404 and RFC 8259, so JSON produced anywhere means the same thing everywhere.
For configuration files that people open and edit, YAML is common. With fewer brackets and quotes, the structure is easy to follow by eye, and comments can record the reasons behind settings. That is why Docker Compose, Kubernetes, GitHub Actions and many CI tools use YAML. Since one space of indentation can change the structure, though, it is a good habit to tidy indentation with the YAML Formatter after editing and check that it parses without errors.
Security is worth a thought too. Feature-rich YAML libraries call for caution when reading untrusted input; Python's PyYAML recommending yaml.safe_load over yaml.load is the classic example. For data received from outside, JSON is the safer choice; for settings a team maintains together, YAML is a comfortable one.
Tools in this post
More posts
- Tech How Was Morse Code Invented? Its History and the Logic of Dots and DashesMorse code history from the 1844 telegraph to International and Korean Morse code: why E is a single dot while Q is long, the timing rules for dots and dashes, and how SOS was chosen.
- Tech How Damaged Can a QR Code Be and Still Scan? Error Correction ExplainedQR code error correction levels L, M, Q and H recover from about 7, 15, 25 and 30% damage. How Reed-Solomon codes make that work, which parts must stay intact, and which level to pick for a logo.
- Tech Is 1 KB 1000 Bytes or 1024 Bytes? KB vs KiB ExplainedWhy 1000-byte KB and 1024-byte KiB coexist, why a 1 TB drive shows up as 931 GB, and how network speeds fit in, worked out with real numbers.