Skip to main content
Hello, and welcome to this lesson.
An orange presentation slide with the title "JSON PATH IN KUBERNETES" in large white text on the left. Faint network/node lines are visible as a background design.
My name is Mumshad Mannambeth. In this lesson we’ll cover how to use JSONPath with kubectl to extract, filter, and format Kubernetes API output. You’ll learn why JSONPath is useful in production environments, how to inspect raw JSON from kubectl, how to build JSONPath queries (including range loops), and how to use custom-columns and --sort-by for clean, sorted output. At the end are practice resources to reinforce the concepts. Prerequisites
A presentation slide titled "Pre-Requisite" listing JSON PATH learning resources and practice tests. A large URL at the bottom links to www.kodekloud.com/p/json-path-quiz.
Before you begin:
  • You should already be familiar with JSONPath basics. If not, complete the JSON Path lessons and practice tests in the JSON Path Test - Free Course.
  • Practice inspecting Kubernetes JSON documents locally so you can design JSONPath expressions against the actual structure.
Why JSONPath? When managing production Kubernetes clusters you often deal with hundreds of nodes and thousands of objects (Pods, Deployments, ReplicaSets, Services, Secrets, etc.). The default kubectl table output is helpful, but the API returns much more detail in JSON. JSONPath lets you extract specific fields (node names, CPU capacity, container images, taints, architecture, etc.) and format them into concise, script-friendly output — ideal for automation, reporting, and quick audits.
A presentation slide titled "Why JSON PATH?" with bullet points about large data sets. It notes "100s of Nodes" and "1000s of PODs, Deployments, ReplicaSets."
How kubectl works (high level)
  • kubectl communicates with the Kubernetes API server (kube-apiserver), which returns objects as JSON.
  • kubectl by default formats that JSON into a human-friendly table.
  • The raw JSON contains many more fields than shown in the default table.
Example: default table output vs raw JSON Default table output (kubectl get nodes):
Raw JSON (kubectl get nodes -o json) — abbreviated:
When you need fields not shown by default (CPU capacity, taints, images, architecture), you can either use kubectl describe for an expanded human-readable view, or use JSONPath to extract exactly the fields you need for scripts and reports. Common output patterns (examples) You might commonly want outputs such as: Four-step approach to using JSONPath with kubectl
  1. Identify the kubectl command to fetch the target resources:
    • Nodes: kubectl get nodes
    • Pods: kubectl get pods
    • Deployments: kubectl get deployments
  2. Inspect the raw JSON:
  1. Examine the JSON structure and design the JSONPath expression that selects the desired field(s). Example to get the first pod container image:
  1. Apply the JSONPath query with kubectl:
Tip: Beginner workflow
  • Run kubectl get <resource> -o json.
  • Paste the JSON into a JSONPath tester or experiment locally.
  • Build and validate the JSONPath expression.
  • Use that expression with kubectl -o=jsonpath='{...}'.
Mapping common resources to commands Common JSONPath examples with kubectl
  • Node names:
  • Node architecture:
  • Node CPU capacity:
Combining queries Concatenating queries without separators produces space-separated values:
Add separators (newlines or tabs) to format output:
Using loops with range (per-item formatting) For per-item tabular output (one node per line), use range: Desired logical steps:
  • For each item in .items:
    • print .metadata.name
    • print a tab \t
    • print .status.capacity.cpu
    • print a newline \n
  • end range
JSONPath template with range:
This prints neatly tab-separated rows, one node per line — ideal for scripting or piping into other tools. Custom columns (clean, aligned column output) When you want a fixed, column-aligned view, -o custom-columns is easier. Note: custom-columns applies the column paths to each item, so you omit the .items prefix. Example:
Sorting with --sort-by kubectl supports --sort-by=<jsonpath> to sort listed items by a property. Provide a JSONPath expression beginning with a dot: Sort by name:
Sort by CPU capacity:
Quoting and shell considerations
Be careful with quoting in shells (especially when using braces, quotes, and backslashes). Use single quotes around the entire -o=jsonpath='...' expression to avoid shell expansion. On some shells you may need to escape special characters or use different quoting strategies.
Putting it all together — useful commands recap
  • Inspect raw JSON:
  • Get all node names:
  • Print node name and CPU per line (tab-separated):
  • Print custom columns:
  • Sort nodes:
If you’re new to JSONPath, follow the four-step approach: get JSON with -o json, craft the JSONPath expression using a JSONPath tester, then apply it with -o=jsonpath='{...}' in kubectl. This approach reduces trial-and-error directly on the cluster and helps you build reliable automation scripts.
That’s it for this lesson. Head over to the practice tests in the JSON Path Test - Free Course to practice extracting and formatting Kubernetes object fields with JSONPath.

Watch Video

Practice Lab