How to Convert YAML to JSON (and Back)
Converting between YAML and JSON looks like it should be a purely mechanical, lossless operation — and 95% of the time, it is. But there are a few real edge cases worth knowing about before you rely on it for something important.
The straightforward direction: YAML → JSON
Any YAML file that uses only the common features — plain strings, numbers, booleans, lists, and nested maps — converts to JSON with zero surprises, because JSON can represent all of those exact same shapes. Comments are the one thing that don't survive: JSON has no comment syntax, so they're simply dropped during conversion.
The trickier direction: JSON → YAML
This direction is also generally safe, though the resulting YAML can look more "expanded" than you might write by hand — a converter will typically default to quoting all strings, or using a particular indent style, rather than guessing which parts you'd want left unquoted for readability. That's a style difference, not a data difference, and running it through Format afterward with your preferred indent size tends to make it much more presentable.
Where things can actually change
- Dates. An unquoted date-looking YAML value like
2026-01-15is often parsed into an actual date object, and depending on the tool, converting that to JSON might render it differently than the original text (JSON has no native date type, so it usually becomes a string). If exact date formatting matters, quote the value in your YAML source. - The Norway problem. As covered in our data types guide, an unquoted
NOcan be read as booleanfalserather than the string "NO" — this will convert to JSON'sfalse, silently changing your data's meaning. Quote any two-letter country or language codes that could be misread this way. - Duplicate keys. If your YAML source has the same key twice at the same level (which some lenient parsers tolerate), only one value survives conversion — usually the last one.
Try it yourself
The best way to build confidence in a conversion is to actually look at the output, not just trust it blindly. Paste your file into the YAML ↔ JSON converter and check that anything unusual — dates, two-letter codes, oddly-formatted numbers — came out the way you expected.