JSON Path Test - Free Course

Lesson

JSON PATH Use case Kubernetes

Hello and welcome! I’m Mumshal Manambath. In this guide, you’ll learn how to leverage JSONPath with the kubectl command to query, filter, and format Kubernetes resources. We’ll cover:

  • Viewing kubectl output in raw JSON
  • Building JSONPath expressions step by step
  • Real-world JSONPath examples
  • Loop constructs (range) for iteration
  • Custom columns and table sorting
  • Practice exercises to reinforce your skills

The image lists objectives related to using JSON PATH in KubeCtl, including topics like viewing JSON output, using JSON PATH, examples, loops, custom columns, sorting, and practice tests. There's also a small diagram showing a KubeCtl command and output.

Prerequisites

You should already have a basic grasp of JSONPath syntax and be comfortable running kubectl commands against a Kubernetes cluster.

Note

If you’re new to JSONPath, try these free resources before continuing:

The image lists prerequisites for a JSON PATH quiz, including resources on YouTube and KodeKloud, and provides a URL for the quiz.

Why Use JSONPath with kubectl?

Managing production Kubernetes clusters often means inspecting hundreds of nodes and thousands of objects—deployments, pods, ReplicaSets, services, secrets, and more. JSONPath lets you:

  • Extract specific fields across many resources
  • Generate concise summaries (e.g., node names with CPU counts)
  • Filter output dynamically based on custom criteria

Manual parsing is error-prone and time-consuming. JSONPath queries with kubectl provide a programmable way to slice and dice cluster data.

The image is a slide titled "Why JSON PATH?" and lists reasons such as handling large datasets, including hundreds of nodes and thousands of PODs, deployments, and ReplicaSets.

Understanding kubectl Output

By default, kubectl formats API responses into human-readable tables. To see the raw JSON payload:

kubectl get nodes -o json

Example output (truncated):

{
  "apiVersion": "v1",
  "items": [
    {
      "kind": "Node",
      "metadata": {
        "name": "master",
        "labels": { … }
      },
      "status": {
        "capacity": {
          "cpu": "4"
        },
        "nodeInfo": {
          "architecture": "amd64"
        }
      }
    }
    // more nodes…
  ]
}

If you prefer more columns in the table view, use:

kubectl get nodes -o wide
NAME     STATUS   ROLES    AGE   VERSION     INTERNAL-IP    OS-IMAGE              KERNEL-VERSION
master   Ready    master   40m   v1.11.3     172.17.0.44    Ubuntu 16.04.2 LTS    4.4.0-18148-generic
node01   Ready    <none>   40m   v1.11.3     172.17.0.63    Ubuntu 16.04.2 LTS    4.4.0-18148-generic

Neither the default table nor kubectl describe lets you build fully custom reports (for example, listing node CPU, taints, and container images in one view). This is where JSONPath shines.

The image shows a Kubernetes-related table with node information, including CPU, taints, architecture, and images, under the title "KubeCtl - JSON PATH."

Getting Started with JSONPath

  1. Identify the base resource:
    kubectl get pods
    
  2. Append -o json to inspect the structure:
    kubectl get pods -o json
    
  3. Draft a JSONPath expression against the JSON. Example:
    .items[0].spec.containers[0].image
    
  4. Run with -o jsonpath and wrap your expression in single quotes:
    kubectl get pods -o jsonpath='{.items[0].spec.containers[0].image}'
    

Tip

Use an online JSONPath evaluator to verify your expression before embedding it in kubectl.

Basic JSONPath Queries

Use CaseCommandOutput
List all node nameskubectl get nodes -o=jsonpath='{.items[*].metadata.name}'master node01
List node architectureskubectl get nodes -o=jsonpath='{.items[*].status.nodeInfo.architecture}'amd64 amd64
List CPU counts on each nodekubectl get nodes -o=jsonpath='{.items[*].status.capacity.cpu}'4 4
Combine name and CPU countkubectl get nodes -o=jsonpath='{.items[*].metadata.name}{.items[*].status.capacity.cpu}'masternode0144

Formatting with Newlines and Tabs

Add "\n" and "\t" for readability:

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

Produces:

master node01
4 4

Iteration Using range

Use range to iterate through items:

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

Output:

master    4
node01    4

Custom Columns with kubectl

Define headers and expressions with -o custom-columns (no need for .items[*]):

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

Sorting Resources

Order your output by any JSON field:

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

Conclusion

You now know how to extract, format, and sort Kubernetes data using JSONPath with kubectl. Practice these examples and try the exercises to master JSONPath queries in your clusters.

Happy querying!

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
JSON PATH Lists