How to convert JSON to Kubernetes YAML manifests

Turn kubectl JSON output or generated JSON into clean, multi-document YAML manifests that are ready to commit and apply.

DevOps By Convert JSON to YAML Team

How to convert JSON to Kubernetes YAML manifests

Kubernetes accepts both JSON and YAML, but almost every repository, Helm chart and tutorial uses YAML. If you have JSON from kubectl get -o json, from an API client or from a code generator, converting it takes a few seconds.

1. Get the JSON

kubectl get deployment web -o json > web.json

Output from kubectl get with several resources is wrapped in a List object whose items array holds the resources.

2. Convert it

Open the converter, then upload or drop web.json. The YAML appears immediately. For a manifest, these options work well:

  • Indent: 2 spaces, the Kubernetes convention.
  • Indent arrays: on or off are both valid; pick what your repository already uses.
  • YAML 1.1 safe: on. Kubernetes tooling uses YAML 1.1 rules, so values such as "yes" or "on" in environment variables must stay quoted.

3. Create multi-document files

Kubernetes lets you keep several resources in one file separated by ---. If your JSON is an array of resources, enable Multi-document and each item becomes its own document:

---
apiVersion: v1
kind: Service
metadata:
  name: web
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web

JSON Lines input, with one resource per line, is detected and converted the same way.

4. Clean up server-side fields

JSON exported from a live cluster contains fields that do not belong in version control, such as status, metadata.uid, metadata.resourceVersion, metadata.creationTimestamp and metadata.managedFields. Remove them before committing. The Drop nulls option also helps with generated JSON that is full of empty values.

5. Validate before applying

kubectl apply --dry-run=client -f web.yaml
kubectl diff -f web.yaml

A dry run catches indentation and type errors, and kubectl diff shows what would change on the cluster.

Common pitfalls

  • Environment values must be strings. value: "8080" needs its quotes, because value: 8080 is rejected.
  • Annotations are strings too, including "true" and "false".
  • Ports and replica counts are numbers, so they must not be quoted.

A converter that preserves JSON types exactly, as ours does, keeps all three correct without any manual editing.

← Blog