JSON ↔ YAML
Convert between JSON and YAML formats. Useful for Kubernetes, Docker Compose, and GitHub Actions configs.
About the JSON ↔ YAML Converter
JSON and YAML both serialise structured data, but they suit different contexts. JSON is strict, compact, and supported by every programming language — it is the standard for APIs and data interchange. YAML is designed for human-readable configuration files: it supports comments, multi-line strings, and a cleaner syntax for nested structures, but is stricter about indentation and has more implicit type conversions to watch out for.
Key differences between JSON and YAML
- Comments — YAML supports # comments; JSON does not.
- Syntax — JSON requires quoted keys and commas. YAML uses indentation; most strings do not need quoting.
- Type inference — YAML infers types (true, false, yes, no are all booleans). JSON requires explicit true/false/null.
- Multi-line strings — YAML has literal (|) and folded (>) block styles. JSON requires escaped \n sequences.
When to use each format
Use JSON for API requests/responses and programmatic data interchange where strict typing matters. Use YAML for developer-facing configuration: Docker Compose, Kubernetes manifests, GitHub Actions, Ansible playbooks, and CI/CD configs where readability and comments are important.
YAML anchors and aliases
YAML supports anchors (&) and aliases (*) for reusing values and blocks within a file, reducing repetition. An anchor marks a value to be reused; an alias references it. This is particularly useful in long configuration files where the same values appear in multiple places.
- Anchor syntax —
default: &defaultsmarks a block that can be reused - Alias syntax —
production: *defaultsinserts the anchored block - Merge key —
<<: *defaultsmerges an anchored mapping into the current one - Limitations — anchors and aliases are YAML-only; they are expanded and lost when converting to JSON
Frequently Asked Questions
pip install pyyaml. Then: import json, yaml; data = json.loads(json_str); print(yaml.dump(data, default_flow_style=False)). To go the other way: data = yaml.safe_load(yaml_str); print(json.dumps(data, indent=2)).