Kubernetes Manifest JSON and YAML Conversion
Convert Kubernetes manifests between YAML and JSON formats. Covers Deployments, Services, and ConfigMaps with practical examples and kubectl tips.
Detailed Explanation
Kubernetes accepts both YAML and JSON for its resource manifests. While YAML is the standard convention in the Kubernetes ecosystem, JSON is useful for programmatic generation, API calls, and debugging.
A Kubernetes Deployment in YAML:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
labels:
app: web
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.25
ports:
- containerPort: 80
resources:
limits:
cpu: "500m"
memory: "128Mi"
requests:
cpu: "250m"
memory: "64Mi"
The JSON equivalent:
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "web-app",
"labels": {
"app": "web",
"tier": "frontend"
}
},
"spec": {
"replicas": 3,
"selector": {
"matchLabels": { "app": "web" }
},
"template": {
"metadata": {
"labels": { "app": "web" }
},
"spec": {
"containers": [
{
"name": "web",
"image": "nginx:1.25",
"ports": [{ "containerPort": 80 }],
"resources": {
"limits": { "cpu": "500m", "memory": "128Mi" },
"requests": { "cpu": "250m", "memory": "64Mi" }
}
}
]
}
}
}
}
Practical tips:
kubectlaccepts both formats:kubectl apply -f deployment.yamlorkubectl apply -f deployment.json- To convert existing resources:
kubectl get deployment web-app -o jsonexports JSON - CPU and memory values like
"500m"and"128Mi"must be quoted in YAML to remain strings - Kubernetes API responses are always JSON, making JSON familiarity essential for debugging
Multi-document YAML (separated by ---) is common in Kubernetes but has no JSON equivalent. Each document must be converted to a separate JSON file or placed in a JSON array.
Use Case
Converting Kubernetes manifests to JSON for use with infrastructure-as-code tools like Pulumi or AWS CDK that generate resources programmatically in TypeScript or Python.