Skip to content
Latchkey

kubectl patch --type=json: JSON Patch Operations

kubectl patch --type=json applies an ordered list of RFC 6902 operations (add, replace, remove, test) addressed by JSON Pointer paths.

When you need to remove a key, edit a specific array index, or change a CRD field by exact path, a JSON patch is the precise choice.

What it does

With --type=json, kubectl patch takes an array of operations, each with an op (add, remove, replace, move, copy, test) and a path written as a JSON Pointer. Array indices are explicit, so you can target /spec/template/spec/containers/0/image exactly.

Common usage

Terminal
kubectl patch deployment api --type=json -p \
  '[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"api:v2"}]'
# remove a field
kubectl patch deploy api --type=json -p \
  '[{"op":"remove","path":"/spec/template/spec/containers/0/livenessProbe"}]'
# append to a list with the - index
kubectl patch deploy api --type=json -p \
  '[{"op":"add","path":"/spec/template/spec/containers/0/env/-","value":{"name":"X","value":"1"}}]'

Options

Field / FlagWhat it does
--type=jsonInterpret the patch as RFC 6902 JSON patch
opadd, remove, replace, move, copy, or test
pathJSON Pointer; ~1 escapes /, ~0 escapes ~ in keys
/list/-The - index appends to an array
--patch-file <file>Read the operations from a file

In CI

JSON patch is the only patch type that can remove a field, and it works on CRDs since it needs no schema. Escape slashes in annotation keys as ~1, for example /metadata/annotations/example.com~1config.

Common errors in CI

"the server rejected our request ... add operation does not apply: doc is missing path" means a parent object in the path does not exist; add it first or use a replace only when present. "remove operation does not apply: doc is missing path" means the field is already absent. "error: unable to parse ... invalid character" usually means unescaped quotes in the shell; pass the patch from a file to avoid quoting headaches.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →