Why convert JSON to YAML?
YAML is the default format for DevOps configuration — Kubernetes manifests, Docker Compose files, Ansible playbooks and GitHub Actions workflows are all written in it. It drops braces, brackets and most quotes, which makes it much easier for people to read and edit than the equivalent JSON.
Example conversion
JSON
{
"apiVersion": "v1",
"kind": "Service",
"metadata": { "name": "web" },
"spec": {
"ports": [
{ "port": 80, "targetPort": 8080 }
]
}
}YAML
apiVersion: v1
kind: Service
metadata:
name: web
spec:
ports:
- port: 80
targetPort: 8080JSON vs YAML syntax
| Type | JSON | YAML |
|---|---|---|
| Object | {"key": "value"} | key: value |
| Array | ["a", "b"] | - a / - b (one per line) |
| String | "hello" | hello (quotes optional) |
| Boolean | true / false | true / false |
| Null | null | null or ~ |
Common use cases
- Kubernetes manifests and Helm values files
- Docker Compose files
- CI/CD pipelines (GitHub Actions, GitLab CI)
- Ansible playbooks
- OpenAPI / Swagger specs
Special characters in YAML
This converter automatically quotes strings that YAML could misread — values that look like numbers, contain a colon, match boolean words such as yes or no, or include characters like #, & and *.
Programmatic conversion
// Node.js (js-yaml)
const yaml = require('js-yaml');
console.log(yaml.dump(JSON.parse(text)));
# Python (PyYAML)
import json, yaml
print(yaml.dump(json.loads(text), sort_keys=False))
# Command line (yq)
yq -P file.json
Pro tips
- Validate your JSON first — invalid input can't be converted.
- Output uses 2-space indentation; YAML never allows tabs.
- JSON has no comments, so add any YAML comments by hand after converting.