Last updated
Common YAML to JSON Use Cases
- Processing Kubernetes manifests with jq or JSON-based tools
- Validating CI/CD pipeline configurations programmatically
- Migrating application config from YAML to JSON format
- Feeding YAML-based infrastructure definitions into JSON APIs
- Learning YAML by seeing the equivalent JSON representation
- Debugging YAML type coercion issues before they reach production
Examples
Example 1: Basic YAML to JSON
A simple YAML configuration file converts to an equivalent JSON object.
Input YAML:
name: my-app
version: "1.0.0"
debug: false
port: 8080
database:
host: localhost
port: 5432
name: mydb
Output JSON:
{
"name": "my-app",
"version": "1.0.0",
"debug": false,
"port": 8080,
"database": {
"host": "localhost",
"port": 5432,
"name": "mydb"
}
}
Example 2: YAML Arrays to JSON Arrays
YAML sequences (arrays) convert directly to JSON arrays.
Input YAML:
servers:
- host: web-01.example.com
port: 80
role: primary
- host: web-02.example.com
port: 80
role: secondary
- host: web-03.example.com
port: 80
role: secondary
allowed_origins:
- https://example.com
- https://app.example.com
- https://admin.example.com
Output JSON:
{
"servers": [
{ "host": "web-01.example.com", "port": 80, "role": "primary" },
{ "host": "web-02.example.com", "port": 80, "role": "secondary" },
{ "host": "web-03.example.com", "port": 80, "role": "secondary" }
],
"allowed_origins": [
"https://example.com",
"https://app.example.com",
"https://admin.example.com"
]
}
Example 3: Kubernetes Manifest to JSON
Kubernetes manifests are written in YAML but kubectl and many tools also accept JSON. Converting enables using jq for processing.
Input YAML (Kubernetes Deployment):
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
namespace: production
labels:
app: my-app
version: "1.0.0"
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0.0
ports:
- containerPort: 8080
Output JSON:
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "my-app",
"namespace": "production",
"labels": { "app": "my-app", "version": "1.0.0" }
},
"spec": {
"replicas": 3,
"selector": { "matchLabels": { "app": "my-app" } },
"template": {
"metadata": { "labels": { "app": "my-app" } },
"spec": {
"containers": [
{
"name": "my-app",
"image": "my-app:1.0.0",
"ports": [{ "containerPort": 8080 }]
}
]
}
}
}
}