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

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.

← Format your YAML now