YAML vs JSON: When to Use Each and How to Convert Safely
YAML and JSON describe the same data structures: objects, lists, strings, numbers and booleans. YAML is designed for people to write by hand, which is why Kubernetes, Docker Compose and CI pipelines use it. JSON is stricter and easier for programs to parse, which is why APIs use it. Converting between them is common when you need to feed a config file into a tool that expects the other format.
Indentation is the most common YAML error
YAML uses spaces for nesting and forbids tabs. A single misaligned space can move a key under the wrong parent without causing an error, so the converted JSON is a good way to check that the structure is what you intended.
Strings that change type
Unquoted values like 1.10, 0755 or 2026-01-01 can be read as numbers or dates. Version numbers are a classic trap: version: 1.10 becomes the number 1.1. Quote any value that must stay a string.
version: "1.10" # string
version: 1.10 # number 1.1Multi-line strings
Use | to keep line breaks exactly (good for scripts and certificates) and > to fold lines into one paragraph. In JSON these become a single string containing \n characters.
Converting on the command line
For scripts and CI, the yq tool converts in one step:
yq -o=json config.yaml > config.json
yq -P config.json > config.yaml