YAML and JSON as configuration languages
Both formats express trees of maps, lists, and scalars. JSON is strict: double-quoted keys and strings, no comments, no trailing commas. YAML adds human-oriented syntax—indentation for nesting, optional quotes, comments, and anchors—while every JSON document is also valid YAML 1.2.
DevOps pipelines (Kubernetes, GitHub Actions, Helm values) and many Spring-style configs prefer YAML; REST mocks, browser devtools, and jq pipelines usually consume JSON. Conversion is about the shared data model, not about preserving YAML-only features such as comments, merge keys, or custom tags.
YAML and JSON — background
Shared data model
- Maps (objects) become JSON objects or YAML mappings keyed by indentation.
- Lists become JSON arrays or YAML sequences with - items.
- Scalars: strings, numbers, booleans, and null map directly when syntax is unambiguous.
- JSON is a syntactic subset of YAML; a .json file parsed as YAML yields the same structure for typical configs.
What YAML adds beyond JSON
- Line comments (# …) and block comments are ignored by JSON entirely.
- Anchors (&), aliases (*), and merge keys (<<) are YAML-specific and do not round-trip to JSON.
- Unquoted strings, multiline literals (| and >), and flexible boolean tokens (yes/no) depend on the YAML engine.
- Multiple documents in one file use --- separators; JSON is one value per document.
Whitespace and typing pitfalls
- YAML nesting is defined by spaces (tabs are discouraged); inconsistent indent breaks parsing.
- Leading-zero IDs and very long digit strings may parse as numbers in YAML but must stay strings in many APIs—quote them in YAML before exporting JSON.
- Multiline | and > blocks become JSON strings with embedded newlines; escaping differs from single-line JSON strings.
- Null, true, and false have YAML and JSON spellings; quoted "true" stays a string in both worlds.
Kubernetes and CI context
- Manifests and workflow files are YAML-first; admission controllers and policy tools often want JSON snapshots for tests.
- Helm values and docker-compose are YAML maps; comparing them to API JSON responses requires the same logical fields, not identical formatting.
- One manifest per conversion keeps --- multi-doc files predictable; merge results in your editor or VCS.
Where conversion fits
Policy and schema checks
OPA, JSON Schema, or OpenAPI examples often assume JSON while clusters store YAML—structural conversion clarifies fields without retyping.
Fixtures across stacks
Share one logical object between Go testdata (YAML), Jest (JSON), and Python configs by converting representation, not semantics.