JSON vs YAML for Config Files: Which Should You Use?
YAML is easier to read and easier to get wrong. Here is what each format gives you for configuration, the mistakes YAML lets through, and a rule for choosing.
JSON vs YAML is mostly a question about configuration files, the one place where the two formats compete directly. YAML is easier to read and lets you write comments, which is why so many deployment and CI tools use it. JSON is stricter, which is why so many programs write it.
The JSON to YAML Converter switches a file between the two, and its page explains which YAML features have no JSON equivalent. This guide is about the choice itself: what YAML costs in practice, and how to produce YAML from JSON without introducing problems. The examples were run with the yaml and js-yaml packages for Node.
What YAML gives you
- Comments. A config file is documentation for whoever edits it next, and JSON has no place for that.
- Less punctuation. No braces, fewer quotes, no commas to forget, so diffs are smaller and easier to review.
- Readable long text. Multi-line strings such as scripts or certificates can be written as blocks instead of one line full of
\n.
What YAML costs
Indentation mistakes change the data silently
In JSON a misplaced brace is a syntax error. In YAML a missing indent is usually valid, just different. Here enabled was meant to sit under tls:
server:
host: localhost
port: 8080
tls:
enabled: trueIt parses without error to {"server": {...}, "tls": null, "enabled": true}. TLS is now off, and nothing complained.
Unquoted values pick their own type
YAML guesses the type of every unquoted value. version: 1.10 is read as the number 1.1, so the version silently changes. And parsers that follow the older YAML 1.1 rules read country: no as false. With the yaml package's default YAML 1.2 rules it stays the string no, but switched to version 1.1 the same file returns false. A file can therefore mean different things to two programs that both say they read YAML.
Comments do not survive a program
Comments are the main reason to choose YAML, and most code that reads and writes a config file throws them away. Parsing to plain data and writing it back loses every comment. The yaml package can keep them, but only if you work with its document API (YAML.parseDocument and doc.toString()) instead of plain objects.
Writing YAML from JSON safely
Never build YAML by joining strings. Use a library, and check how it quotes ambiguous values:
import { dump } from "js-yaml";
dump({ country: "no", version: "1.10", zip: "007", note: "a: b" });
// country: 'no'
// version: '1.10'
// zip: '007'
// note: 'a: b'js-yaml quotes all four, so any reader gets strings back. The yaml package's YAML.stringify quotes 1.10, 007 and a: b but writes country: no without quotes, which is correct for YAML 1.2 readers and wrong for YAML 1.1 readers. If the file goes to tools you do not control, prefer output that quotes such values. Note that js-yaml 5 has no default export, so use the named import shown above.
JSON or YAML: a rule for choosing
- Use JSON when programs write the file or several languages read it. Every parser agrees on what it means.
- Use YAML when people edit the file often and comments matter, and the tools that read it already expect YAML.
- With YAML, quote strings that look like numbers, booleans or dates, and validate the parsed result against a schema in CI, so an indentation slip fails a build instead of a deployment.
- Do not hand-edit generated YAML. Change the source and regenerate, or the next generation overwrites the edit.
Related tools
Parse any YAML config into strict, valid JSON your code can consume.
Flatten an array of JSON objects into spreadsheet-ready CSV.
Pretty-print JSON with configurable indentation and instant validation.
Convert JSON into well-formed XML with attribute and element control.
Related guides
Two lines of code convert YAML to JSON, and the parser's defaults decide whether merge keys apply, dates stay strings and no means false. Here are the options that matter, tested.
Joining values with commas breaks on the first name that contains one. Here is a short, tested JSON to CSV converter for JavaScript and Python, and the details that make Excel open it correctly.
Two formatters can disagree about the same JSON file. Here is how a team picks one, applies it in every editor and before every commit, and makes CI enforce it.
A library escapes the text for you, but it will happily write XML that is not valid. Here is tested JSON to XML code for Node and Python, and the three inputs that break it.