Converting JSON to YAML for Kubernetes Configurations
Why JSON → YAML for Kubernetes?
Kubernetes accepts both JSON and YAML, but YAML is the community standard:
it supports comments, multi-line strings, and is less noisy to read.
Most tools (kubectl apply, Helm, Kustomize) expect YAML by default.
When you generate Kubernetes configs programmatically — from a UI, a script, or an API — the output is often JSON. Converting to YAML before committing keeps your repository readable.
Converting via API
curl -X POST https://api.toolkitapi.io/v1/convert/json-to-yaml \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"json": {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": { "name": "my-app" },
"spec": { "replicas": 3 }
}
}'
Response:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
Preserving Key Order
Kubernetes manifests expect apiVersion and kind at the top.
The API preserves insertion order from the input JSON so the output
matches the expected manifest structure.
Multi-Document Output
Pass an array at the top level to get a YAML file with --- document separators,
matching the format used for kubectl apply -f:
[
{ "apiVersion": "v1", "kind": "Service", ... },
{ "apiVersion": "apps/v1", "kind": "Deployment", ... }
]
Automating Manifest Generation
import httpx, yaml as pyyaml
manifests = build_k8s_manifests() # returns list of dicts
resp = httpx.post(
"https://api.toolkitapi.io/v1/convert/json-to-yaml",
headers={"X-API-Key": API_KEY},
json={"json": manifests, "multi_document": True},
)
Path("k8s/generated.yaml").write_text(resp.json()["yaml"])