YAML to JSON for Kubernetes — the API server's view
Cluster config lives in YAML, and half the tooling wants JSON. Convert a manifest — multi-document file and all — and see the exact JSON the API server would receive.
The commands that do it in a terminal
If you have the cluster tooling, you may not need a converter at all. These are the commands that Kubernetes users reach for first.
# One JSON document per resource, separated by newlines.
yq -o=json '.' deployment.yaml
# Every document in a multi-document manifest.
yq -o=json -s '.' ./manifests/
yq - the quickest way, from a file
import json
import yaml
# safe_load_all handles the "---" separators in a multi-document manifest.
with open("deployment.yaml", encoding="utf-8") as f:
documents = list(yaml.safe_load_all(f))
print(json.dumps(documents, indent=2))
Python - multi-document manifests without extra dependencies
# What the API server actually stores, as JSON.
kubectl get deployment my-app -o json
# As YAML instead.
kubectl get deployment my-app -o yaml
kubectl - straight from the cluster
safe_load_all is the piece people miss: a Kubernetes manifest with several resources separated by --- is a multi-document YAML file, and yaml.safe_load raises an error on it. The converter above handles the same case and returns one JSON object per resource.
The other thing worth knowing is that kubectl get -o json gives you the live object, with every default the API server filled in - creationTimestamp, resourceVersion, uid - not just the fields you wrote. A YAML to JSON conversion gives you exactly what is in your file.
A worked example
A minimal Deployment manifest, and the JSON it becomes.
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
labels:
app: web
spec:
replicas: 2
template:
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
deployment.json
[
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "web",
"labels": {
"app": "web"
}
},
"spec": {
"replicas": 2,
"template": {
"spec": {
"containers": [
{
"name": "web",
"image": "nginx:1.27",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
}
]
Note what did not change: replicas: 2 stays the number 2, containerPort: 80 stays the number 80, and app: web stays a string. That matters in Kubernetes, because the API server rejects a manifest where a number arrived as a quoted string. The output is an array because a manifest file can hold several documents, and the converter returns one element per document - even for a file that holds only one.
Convert a manifest in the browser
Useful when the manifest is on your clipboard, or when you cannot reach the cluster from this machine.
-
Paste the YAML
Paste the whole file, including the "---" separators if it holds several resources.
-
Read the JSON
The output panel shows the JSON for every document, with two-space indentation so it is readable.
-
Check the types
Look at the fields you care about - replicas, ports, memory limits. They keep their YAML type.
-
Copy it into your pipeline
Paste it into kubectl apply, a Helm template, a CI job, or a policy check.
Built for how manifests are actually written
The details that separate a YAML parser that works on manifests from one that does not.
Multi-document files
A manifest with several resources separated by --- is converted completely, not just the first document.
Types stay intact
Numbers remain numbers and quoted strings remain strings, which is what the API server validates against.
Anchors resolved
YAML anchors and aliases are expanded for you, so a shared block is present in every document that uses it.
Manifests stay private
A manifest often contains image pull secrets and internal hostnames. It is parsed in your browser and never sent.
The full YAML to JSON guide
YAML syntax, the types it produces, multi-document files and the quoting rules that catch people out.
Related tools: YAML to JSON converter · JSON to YAML converter · JSON to SQL · JSON to CSV
Frequently asked questions
Multi-document files, types and the commands that already do this in a terminal.
How do I convert a Kubernetes manifest to JSON?
yq -o=json "." deployment.yaml in a terminal, or kubectl get deployment my-app -o json to pull the live object from the cluster.What about multi-document YAML files?
yaml.safe_load_all - safe_load fails on a multi-document file.Do I need kubectl or a cluster?
Which YAML values need care?
Does it handle anchors and aliases?
Convert your manifest now
Free, instant, and private — your manifest never leaves this tab.