YAML vs JSON: when to use each
Short answer
Use JSON for data exchanged between programs, especially APIs, because it is strict and universally supported. Use YAML for configuration that people edit by hand, because it is less noisy and supports comments. They can represent the same data.
Same data, different style
YAML is actually a superset of JSON, so any JSON document is valid YAML. The difference is ergonomics: JSON uses braces, brackets, and quotes; YAML uses indentation and is lighter to read and write.
JSON: YAML:
{ name: Ada
"name": "Ada", roles:
"roles": ["author"] - authorWhat YAML adds
- Comments with
#, which JSON does not support - Less punctuation, so configuration is easier to scan
- Anchors and references to reuse repeated blocks
Where YAML bites
- Indentation is significant, so a stray space can change the meaning
- Some unquoted values are surprising, for example a bare
nocan be read as false - It is harder for machines to parse quickly and consistently than JSON