skip to content
Alvin Lucillo

Using jsonpath to query k8s

/ 1 min read

Suppose you have these pods and you want to get the image of used by the pods:

k get po
NAME     READY   STATUS    RESTARTS   AGE
nginx    1/1     Running   0          8m26s
nginx2   1/1     Running   0          62s

We specify the JSON path in jsonpath. To understand the structure of k8s resource in JSON format, try to query the resources via k get po -o json

kubectl get pods -o jsonpath='{.items[*].metadata.name} {.items[*].spec.containers[*].image}'
nginx nginx2 nginx nginx

The query above is hard to read, so we use range to perform a loop, and print a newline.

kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name} {.spec.containers[*].image}{"\n"}{end}'
nginx nginx
nginx2 nginx