YAML Is a Superset, So One Direction Can Fail
Every JSON document is valid YAML, which makes JSON → YAML always safe. The reverse is not guaranteed: YAML has features JSON has no representation for, and a document using them cannot round-trip. Watch for anchors and aliases (&name / *name), which are expanded inline; non-string mapping keys, which JSON requires to be strings; multiple documents separated by ---; and explicit tags such as !!binary.
The Norway Problem
YAML 1.1 treats yes, no, on, off, y, and n as booleans, which is why a country list containing NO for Norway parses as false. Quote such values in the YAML source — "NO" — if you want a string. Converting to JSON is a quick way to see which reading your YAML actually gets.
Multi-line Scalars Become Escaped Strings
description: |
line one
line two
becomes {"description": "line one\nline two"}. The literal block indicator | keeps the newlines; the folded indicator > joins the lines with spaces instead.
On the Command Line
# yq
yq -o=json '.' manifest.yaml
# Python
python3 -c "import sys,yaml,json; json.dump(yaml.safe_load(sys.stdin), sys.stdout, indent=2)" < manifest.yamlWho this guide is for
- Convert a Kubernetes manifest to JSON for an API call to the cluster
- Turn a Docker Compose file into JSON for programmatic inspection
- Feed a YAML CI pipeline into a tool that only reads JSON
- Inspect an ambiguous YAML value by seeing how it parses