Skip to main content
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.
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:
Example output (truncated):
If you prefer more columns in the table view, use:
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:
  2. Append -o json to inspect the structure:
  3. Draft a JSONPath expression against the JSON. Example:
  4. Run with -o jsonpath and wrap your expression in single quotes:
Use an online JSONPath evaluator to verify your expression before embedding it in kubectl.

Basic JSONPath Queries

Formatting with Newlines and Tabs

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

Iteration Using range

Use range to iterate through items:
Output:

Custom Columns with kubectl

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

Sorting Resources

Order your output by any JSON field:

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

Practice Lab