Kubernetes JSON to YAML Conversion

Convert Kubernetes JSON to YAML using kubectl get -o yaml, yq -P, or our online converter. Kubernetes accepts both formats, but YAML is the community standard for manifests. This guide covers kubectl json yaml conversion, manifest best practices, and when to use each format. See our YAML vs JSON comparison for a detailed breakdown.

kubectl JSON and YAML Output

kubectl supports both JSON and YAML output formats:

Get Resources as YAML (Default)

# Get deployment as YAML
kubectl get deployment myapp -o yaml

# Get all pods as YAML
kubectl get pods -o yaml

# Get specific pod and save to file
kubectl get pod nginx-xyz -o yaml > pod.yaml

Get Resources as JSON

# Get deployment as JSON
kubectl get deployment myapp -o json

# Get with jq filtering
kubectl get pods -o json | jq '.items[].metadata.name'

# JSONPath for specific fields
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
Tip: Use -o yaml for human-readable output and -o json for piping to tools like jq. For Helm charts, YAML is always used.

How to Convert Kubernetes JSON to YAML

Method 1: yq (Recommended)

# Install yq
brew install yq         # macOS
sudo apt install yq     # Ubuntu

# Convert JSON file to YAML
yq -P deployment.json > deployment.yaml

# Pipe kubectl output
kubectl get deployment -o json | yq -P > deployment.yaml

# In-place conversion (creates backup)
yq -P -i deployment.json

Method 2: kubectl Directly

# Just use -o yaml instead of -o json
kubectl get deployment myapp -o yaml > deployment.yaml

# Don't convert from JSON - get YAML directly from API

Method 3: Python Script

import json
import yaml

# Convert k8s JSON to YAML
with open('deployment.json') as f:
    data = json.load(f)

with open('deployment.yaml', 'w') as f:
    yaml.dump(data, f, default_flow_style=False)

Method 4: Online Converter

Use our free online converter - paste your JSON and get YAML instantly (works for any valid JSON including Kubernetes manifests).

Working with Kubernetes Manifests

YAML Manifest Example

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.24
        ports:
        - containerPort: 80

Same Manifest in JSON

{
  "apiVersion": "apps/v1",
  "kind": "Deployment",
  "metadata": {
    "name": "nginx-deployment",
    "labels": {
      "app": "nginx"
    }
  },
  "spec": {
    "replicas": 3,
    "selector": {
      "matchLabels": {
        "app": "nginx"
      }
    },
    "template": {
      "metadata": {
        "labels": {
          "app": "nginx"
        }
      },
      "spec": {
        "containers": [
          {
            "name": "nginx",
            "image": "nginx:1.24",
            "ports": [
              {
                "containerPort": 80
              }
            ]
          }
        ]
      }
    }
  }
}
Notice: The YAML version is 30% shorter and allows comments. This is why YAML is preferred for k8s manifests.

JSON vs YAML for Kubernetes

Feature YAML JSON
Community standard ✅ Yes No
Comments ✅ # comments ❌ Not allowed
Readability ✅ Excellent Good
File size ✅ Smaller Larger
Multi-document ✅ --- separator ❌ No
jq/jsonpath Needs conversion ✅ Native
API communication Converted to JSON ✅ Native

When to Use Each

  • Use YAML: Writing manifests, documentation, GitOps
  • Use JSON: Scripting, jq filtering, API debugging

Helm Chart Conversion

Helm charts use YAML extensively. Here's how to work with Helm and JSON:

# Render Helm chart to YAML
helm template myrelease ./mychart > manifests.yaml

# Get Helm values as JSON
helm get values myrelease -o json

# Install with JSON values file
helm install myrelease ./mychart -f values.json

# Convert values.yaml to JSON for scripting
yq -o=json values.yaml > values.json
Helm tip: Helm accepts both YAML and JSON for values files, but templates must be YAML. Use helm template to preview rendered manifests.

Frequently Asked Questions

Does Kubernetes use JSON or YAML?

Kubernetes accepts both JSON and YAML, but YAML is the community standard. Internally, the API server uses JSON for all communication. Most k8s manifests are written in YAML because it's more readable and supports comments. kubectl outputs YAML by default with -o yaml.

How do I convert kubectl JSON output to YAML?

Use the -o yaml flag instead of -o json: kubectl get deployment myapp -o yaml. To convert an existing JSON file to YAML, use yq: yq -P deployment.json > deployment.yaml. Or pipe directly: kubectl get pod -o json | yq -P.

Can I apply JSON directly to Kubernetes?

Yes, kubectl apply works with both JSON and YAML files. Just run: kubectl apply -f manifest.json. Kubernetes detects the format automatically. However, YAML is preferred because you can add comments to document your manifests.

Which is better for Kubernetes: JSON or YAML?

YAML is better for Kubernetes manifests for several reasons: it's more readable, supports comments for documentation, uses fewer characters (no quotes on keys), and is the community standard. All official k8s documentation uses YAML examples.

Need to convert JSON to YAML?

Use our free online converter—works for any JSON including k8s manifests.

Open Converter Tool →