Skip to content

13.01 JSON Path in Kubernetes

JSONPath in Kubernetes is used with kubectl to extract specific fields from large Kubernetes API outputs. It is useful for CKA tasks, production troubleshooting, reporting, and filtering large clusters with many Pods, Nodes, Deployments, ReplicaSets, Services, and Secrets.

Goal

Use JSONPath with kubectl to:

  • inspect Kubernetes objects in JSON format
  • extract specific fields
  • print custom reports
  • loop through lists
  • create custom columns
  • sort output using object fields

Why JSONPath?

In production clusters, you may deal with:

  • hundreds of Nodes
  • thousands of Pods
  • many Deployments, ReplicaSets, Services, Secrets, ConfigMaps, and PVCs

Reading full YAML or JSON manually is slow. JSONPath helps you extract only the fields you need.

Use cases

  • Print all Node names
  • Print Node CPU capacity
  • Print Pod images
  • Print Node taints
  • Print architecture of each Node
  • Sort Pods or Nodes by a field
  • Create custom table output for quick troubleshooting

Tip

Use JSONPath when built-in outputs like kubectl get, kubectl get -o wide, or kubectl describe do not show the field in the format you need.


How kubectl Output Works

kubectl talks to the Kubernetes API server. The API server returns data in JSON. By default, kubectl formats that JSON into a human-readable table.

kubectl get nodes

Example output:

NAME     STATUS   ROLES    AGE   VERSION
master   Ready    master   40m   v1.11.3
node01   Ready    <none>   40m   v1.11.3

To see the raw JSON:

kubectl get nodes -o json

Note

JSONPath queries are written against the raw JSON structure, not against the default table output.


Basic JSONPath Workflow

Follow this method when building JSONPath commands.

1. Identify the kubectl command
2. View the output as JSON
3. Build the JSONPath query
4. Run kubectl with -o jsonpath
kubectl get nodes
kubectl get pods
kubectl get deployments
kubectl get nodes -o json
kubectl get pods -o json
.items[0].metadata.name
.items[0].spec.containers[0].image
kubectl get pods -o=jsonpath='{.items[0].spec.containers[0].image}'

Warning

The JSONPath query should be wrapped in:

'{ ... }'

Example:

kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'

Important JSON Structure

Most kubectl get <resource> -o json outputs contain a top-level items list.

{
  "apiVersion": "v1",
  "items": [
    {
      "kind": "Node",
      "metadata": {
        "name": "master"
      },
      "status": {
        "capacity": {
          "cpu": "4"
        }
      }
    }
  ],
  "kind": "List"
}

Common paths:

Field JSONPath
First item name .items[0].metadata.name
All item names .items[*].metadata.name
First Pod first container image .items[0].spec.containers[0].image
All Node CPU capacity .items[*].status.capacity.cpu
All Node architecture .items[*].status.nodeInfo.architecture

Tip

Use [*] when you want all objects in a list. Use [0], [1], etc. when you want a specific item by index.


Basic JSONPath Examples

kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'

Output:

master node01
kubectl get nodes -o=jsonpath='{.items[*].status.nodeInfo.architecture}'

Output:

amd64 amd64
kubectl get nodes -o=jsonpath='{.items[*].status.capacity.cpu}'

Output:

4 4
kubectl get pods -o=jsonpath='{.items[0].spec.containers[0].image}'

Output:

nginx

Pod image path

For Pods, image details are usually under:

.items[*].spec.containers[*].image

Formatting Output

By default, JSONPath output is usually printed on one line. Add newline and tab characters to make it readable.

Format Meaning
{"\\n"} new line
{"\\t"} tab
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}{"\\n"}{.items[*].status.capacity.cpu}'

Output:

master node01
4 4
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}{" "}{.items[*].status.capacity.cpu}'

Output:

master node01 4 4

Note

{"\\n"} and {"\\t"} are formatting strings inside the JSONPath expression.


Loops with range

Use range when you want clean row-by-row output.

kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\\t"}{.status.capacity.cpu}{"\\n"}{end}'

Output:

master   4
node01   4

Explanation:

{range .items[*]}       for each item
{.metadata.name}        print node name
{"\\t"}                 print tab
{.status.capacity.cpu}  print CPU count
{"\\n"}                 print new line
{end}                   end loop

CKA-friendly pattern

Memorize this format:

kubectl get <resource> -o=jsonpath='{range .items[*]}{.<field1>}{"\\t"}{.<field2>}{"\\n"}{end}'

JSONPath for Custom Columns

custom-columns is often easier than complex JSONPath loops when you want table output.

Syntax

kubectl get <resource> -o=custom-columns=<COLUMN-NAME>:<JSON-PATH>

Note

With custom-columns, do not include .items[*]. Kubernetes applies the path to each item automatically.

Node and CPU Custom Columns

kubectl get nodes -o=custom-columns=NODE:.metadata.name,CPU:.status.capacity.cpu

Output:

NODE     CPU
master   4
node01   4

Node and Architecture

kubectl get nodes -o=custom-columns=NODE:.metadata.name,ARCH:.status.nodeInfo.architecture

Output:

NODE     ARCH
master   amd64
node01   amd64

Pod and Image

kubectl get pods -o=custom-columns=POD:.metadata.name,IMAGE:.spec.containers[*].image

Output:

POD                         IMAGE
nginx-557945897-gznjp       nginx:alpine

Success

Use custom-columns when you need clean output quickly during an exam.


JSONPath for Sorting

Use --sort-by to sort resources by a JSON field.

Sort Nodes by Name

kubectl get nodes --sort-by=.metadata.name

Sort Nodes by CPU Capacity

kubectl get nodes --sort-by=.status.capacity.cpu

Sort Pods by Name

kubectl get pods --sort-by=.metadata.name

Sort Pods by Creation Time

kubectl get pods --sort-by=.metadata.creationTimestamp

Tip

Sorting is useful when checking oldest/newest Pods, Nodes, or resources during troubleshooting.


Common Kubernetes JSONPath Queries

Requirement Command
Node names kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'
Node CPU kubectl get nodes -o=jsonpath='{.items[*].status.capacity.cpu}'
Node architecture kubectl get nodes -o=jsonpath='{.items[*].status.nodeInfo.architecture}'
Pod names kubectl get pods -o=jsonpath='{.items[*].metadata.name}'
Pod IPs kubectl get pods -o=jsonpath='{.items[*].status.podIP}'
Pod images kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].image}'
Node internal IPs kubectl get nodes -o=jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'
Service ClusterIPs kubectl get svc -o=jsonpath='{.items[*].spec.clusterIP}'
PVC storage requests kubectl get pvc -o=jsonpath='{.items[*].spec.resources.requests.storage}'

Useful Examples for CKA

kubectl get pods -o=custom-columns=POD:.metadata.name,IP:.status.podIP
kubectl get pods -o=custom-columns=POD:.metadata.name,NODE:.spec.nodeName
kubectl get nodes -o=custom-columns=NODE:.metadata.name,TAINTS:.spec.taints[*].key
kubectl get nodes -o=custom-columns=NODE:.metadata.name,INTERNAL-IP:.status.addresses[0].address
kubectl get pods -A -o=custom-columns=NAMESPACE:.metadata.namespace,POD:.metadata.name
kubectl get pods -o=custom-columns=POD:.metadata.name,CONTAINER:.spec.containers[*].name,IMAGE:.spec.containers[*].image

Warning

If a Pod has multiple containers, [*] may print multiple values in the same cell. For exact formatting, use range.


JSONPath with Conditions

You can filter lists using conditions.

Get Internal Node IPs

kubectl get nodes -o=jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'

Get External Node IPs

kubectl get nodes -o=jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

Note

Conditional filters are helpful, but in CKA tasks custom-columns and basic paths are usually enough.


JSONPath vs custom-columns vs sort-by

Option Best for Example
-o=jsonpath exact field extraction and formatting print Pod images
-o=custom-columns readable table reports Node + CPU table
--sort-by sorting normal kubectl output sort Pods by age/name
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'
kubectl get nodes -o=custom-columns=NODE:.metadata.name,CPU:.status.capacity.cpu
kubectl get nodes --sort-by=.metadata.name

Production Best Practices

Do

  • Use -o json first to inspect the object structure.
  • Use custom-columns for readable reports.
  • Use range for clean row-by-row output.
  • Use --sort-by when checking oldest/newest resources.
  • Save frequently used JSONPath commands as shell aliases or runbooks.
  • Use JSONPath in troubleshooting scripts and audit checks.

Don't

  • Do not guess paths without checking -o json.
  • Do not use JSONPath when a simple kubectl get output is enough.
  • Do not rely only on item indexes like [0] in production scripts unless order is guaranteed.
  • Do not expose sensitive fields from Secrets in shared terminals or logs.
  • Do not confuse container name with image name.

Common Mistakes

Mistake 1: Missing quotes

Wrong:

kubectl get nodes -o=jsonpath={.items[*].metadata.name}

Better:

kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'

Mistake 2: Using items in custom-columns

Wrong:

kubectl get nodes -o=custom-columns=NODE:.items[*].metadata.name

Correct:

kubectl get nodes -o=custom-columns=NODE:.metadata.name

Mistake 3: Wrong list index

If you use:

.items[0]

you only get the first object. Use:

.items[*]

when you want all objects.


Quick Revision Cheat Sheet

# Raw JSON
kubectl get nodes -o json
kubectl get pods -o json

# All Node names
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'

# All Pod images
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].image}'

# Node name + CPU using range
kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\\t"}{.status.capacity.cpu}{"\\n"}{end}'

# Custom columns
kubectl get nodes -o=custom-columns=NODE:.metadata.name,CPU:.status.capacity.cpu

# Sort
kubectl get nodes --sort-by=.metadata.name
kubectl get pods --sort-by=.metadata.creationTimestamp

Exam Strategy

Quote

In CKA, JSONPath is mainly about speed. First inspect with -o json, then extract only what the question asks for.

Recommended approach:

1. Run kubectl get <resource> -o json
2. Find the field path
3. Test with -o=jsonpath
4. Use custom-columns if table output is required
5. Use --sort-by if ordering is required

Best CKA habit

When asked to find or print a specific field, avoid manually scanning kubectl describe. Use JSONPath or custom columns for faster and cleaner answers.