YAML vs JSON: practical differences
Published 2026-09-10 · FileType Converters engineering
JSON and YAML both represent structured data, but they optimize for different work. JSON has stricter syntax, fewer implicit features, and is the safer interchange format for APIs. YAML is more pleasant for human-written configuration, but indentation, implicit typing, anchors, and parser differences can create surprises.
Syntax and readability
JSON uses braces, brackets, quotes, commas, and a small set of value types. It is noisy for hand-written configuration but easy for programs to parse consistently. Comments are not part of standard JSON.
YAML uses indentation and a lighter surface syntax. It supports comments, multi-line strings, anchors, aliases, and several scalar styles. That makes it attractive for configuration files, but the same flexibility creates more cases where a file looks right and parses differently than expected.
Typing surprises
Older YAML schemas are known for implicit conversions. Strings such as yes, no, on, off, or date-looking values may be interpreted as booleans or timestamps by some parsers. YAML 1.2 is closer to JSON for booleans, but not every tool uses the same rules.
JSON has fewer surprises because strings must be quoted and booleans are exactly true or false. Numbers still need care, especially for big integers and decimals, but a JSON parser has less room to guess.
Conversion behavior
YAML to JSON requires resolving YAML features into plain JSON values. Comments disappear. Anchors are expanded. Non-string mapping keys may be converted or rejected because JSON object keys are strings. Special numeric values may not have a portable JSON equivalent.
JSON to YAML is usually safer because JSON is largely a subset of YAML 1.2. The output may be more readable, but it is still data, not necessarily a good hand-written config. Review quoting around IDs, dates, and values such as on if humans will edit it later.
Which to choose
Use JSON for APIs, browser data, logs that need strict parsing, and interchange between unrelated systems. Use YAML for human-edited configuration when comments and readability matter and when the parser is known.
For tabular data, neither YAML nor JSON automatically solves flattening. If the destination is Excel, choose CSV or XLSX after deciding how nested objects and arrays should become columns.
Operational checklist
Security-sensitive YAML deserves extra care. Some language-specific YAML loaders have historically supported object construction features that are unsafe with untrusted input. Use safe loaders, restrict schemas, and avoid treating arbitrary YAML uploads as harmless configuration text.
For generated files, prefer JSON unless a human will edit the result. Machines do not need comments, anchors, or indentation conveniences. A generated YAML file can be harder to diff if the emitter changes quoting or line wrapping between versions.
Round-trip tests are useful. Convert YAML to JSON, then back to YAML, and compare the parsed data rather than the text. Comments and formatting will differ, but data changes reveal unsupported YAML features or accidental type conversion.
Final checks
When reviewing converted output, search for booleans, dates, and empty values first. Those are the places where YAML and JSON readers most often disagree.
A final configuration rule should say which parser and YAML version the project supports. Without that, one developer may validate a file locally while the deployment parser reads the same scalars differently.
Questions
Is YAML a superset of JSON?
YAML 1.2 was designed so JSON documents are valid YAML, but real parser behavior still varies.
Do YAML comments survive JSON conversion?
No. JSON has no standard comment syntax, so comments are lost when YAML is converted to JSON.
Why did `on` become true in my YAML?
Some YAML parsers and schemas treat words such as on, off, yes, and no as booleans. Quote them when they must remain strings.
Comments and configuration
YAML comments are useful for configuration maintained by people. JSON’s lack of comments is one reason projects use JSONC, TOML, or YAML for settings instead. When the file is an API payload, however, comments are usually a liability because receivers expect strict data.
For config, YAML also permits anchors and aliases that reduce repetition. That can help large files, but it can confuse simple converters because the expanded data is not always visible in the source text.