Why Convert JSON to YAML?
YAML is the preferred format for configuration files in DevOps tools like Kubernetes, Docker Compose, Ansible, and GitHub Actions. While JSON works programmatically, YAML is more readable for human-edited config files.
Example Conversion
JSON:
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "my-service"
},
"spec": {
"ports": [
{ "port": 80, "targetPort": 8080 }
]
}
}
YAML:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
ports:
- port: 80
targetPort: 8080
JSON vs YAML Syntax
| JSON | YAML |
|---|
{ "key": "value" } | key: value |
["a", "b", "c"] | - a
- b
- c |
"string" | string (quotes optional) |
true / false | true / false (or yes/no) |
null | null or ~ |
Common Use Cases
- Kubernetes manifests — Convert JSON API responses to YAML configs
- Docker Compose — Create docker-compose.yml from JSON templates
- CI/CD pipelines — GitHub Actions, GitLab CI, CircleCI configs
- Ansible playbooks — Convert JSON data to YAML format
- Helm charts — Generate values.yaml files
Special Characters in YAML
The converter automatically quotes strings that could be misinterpreted:
- Strings starting with numbers:
"123abc" - Strings containing colons:
"host:port" - Boolean-like strings:
"yes", "no", "true" - Strings with special characters:
#, &, *
Programmatic Conversion
JavaScript (Node.js)
const yaml = require('js-yaml');
const jsonData = { name: 'example', count: 42 };
const yamlString = yaml.dump(jsonData);
console.log(yamlString);
Python
import json
import yaml
json_data = {"name": "example", "count": 42}
yaml_string = yaml.dump(json_data, default_flow_style=False)
print(yaml_string)
Command Line (with yq)
# Using yq
cat data.json | yq -P > data.yaml
# Or directly
yq -P data.json
Pro Tips
- 💡 Validate your JSON first — Use the JSON Validator to catch syntax errors before converting
- 💡 Check indentation — YAML is indentation-sensitive. Our output uses 2-space indentation (Kubernetes standard)
- 💡 Comments don't transfer — JSON doesn't support comments, so add them manually after converting
Related Tools