Skip to main content
A presentation slide titled "JSON Path in Kubernetes" with a code/document icon on a dark navy curved panel to the right. Small copyright text "KodeKloud" appears in the bottom-left corner.
This guide shows how to use JSONPath with kubectl to extract, filter, and format information from Kubernetes API JSON output. This article covers:
  • Why JSONPath is useful in Kubernetes.
  • How kubectl exposes JSON output from the API server.
  • A step-by-step workflow to build JSONPath queries.
  • Practical examples: range loops, custom columns, sorting.
  • Best practices and testing recommendations.
Before you begin, you should already be familiar with basic JSONPath syntax. If this is new, consider an introductory JSONPath tutorial (for example on YouTube) or KodeKloud practice labs to inspect JSON structures interactively.
A dark-blue presentation slide titled "Prerequisite." It shows three rounded white bars listing JSONPath resources: a YouTube beginners tutorial and two KodeKloud practice tests (one on a Kubernetes dataset).
Why use JSONPath?
A presentation slide titled "Why JSON Path?" with a highlighted bullet about handling large datasets, noting "100s of Nodes" and "1000s of PODs, Deployments, ReplicaSets."
In production clusters you may need to analyze hundreds of nodes and thousands of objects (Pods, Deployments, ReplicaSets, Services, Secrets, etc.). kubectl provides convenient human-readable tables, but those default views hide many fields. JSONPath lets you query the raw JSON from the Kubernetes API to:
  • Extract specific fields across many objects.
  • Create compact reports (name → image, name → CPU, name → taints).
  • Feed results into scripts or monitoring tools.
How kubectl and JSONPath work (overview)
  • kubectl queries the Kubernetes API server.
  • The API server returns JSON documents for the requested resources.
  • kubectl formats those documents as tables by default; use -o json to see full JSON.
  • JSONPath expressions let you extract parts of that JSON; pass them with -o jsonpath='<expr>'.
Start by inspecting the raw JSON to discover paths and structure. Quick example: default vs raw JSON
Request raw JSON:
Sample response snippet:
If you need more detail than kubectl get -o wide shows, -o json and kubectl describe expose fields like status.capacity, spec.taints, status.conditions, and more. Common use cases Step-by-step workflow to build JSONPath queries
  1. Identify the kubectl command that returns the resource: kubectl get nodes, kubectl get pods, etc.
  2. Inspect JSON output: kubectl get <resource> -o json to learn the object structure.
  3. Draft the JSONPath expression (test with online JSONPath evaluators or directly with kubectl).
  4. Run kubectl get <resource> -o jsonpath='<your-expression>'. Wrap the expression in single quotes to avoid shell interpolation.
Start by running kubectl get <resource> -o json to inspect the structure. Finding the correct path in the JSON before writing the JSONPath expression saves time.
Example: Extract a container image from a Pod
  1. Inspect the pod JSON:
Relevant JSON snippet:
  1. JSONPath for the first container image: .items[0].spec.containers[0].image
  2. Use with kubectl:
Useful JSONPath patterns with kubectl get nodes
  • Names of all nodes:
  • Architecture of all nodes:
  • CPU capacity of all nodes:
Combine queries (inline):
Formatting output: newlines and tabs Insert literal newline (\n) and tab (\t) characters inside JSONPath output by embedding them as quoted strings in the expression:
Iterating items with range The JSONPath range construct produces row-based output (one resource per line) which is ideal for human-readable reports:
Breakdown:
  • range .items[*] — loop over each array element.
  • {.metadata.name} — print name.
  • {"\t"} — tab separator.
  • {.status.capacity.cpu} — print CPU capacity.
  • {"\n"} — newline after each row.
  • {end} — close loop.
If you find escaping difficult, build and test expressions incrementally using -o json or an external JSONPath tester. Custom columns with -o custom-columns For simple tabular output, -o=custom-columns is often easier. Note: paths are relative to the item (no leading .items):
Sorting results with --sort-by Sort resources by a JSONPath expression (path should start with a dot and reference fields of the item):
  • Sort by name:
  • Sort and show custom columns:
Quick reference examples
  • Get pod images across all pods:
  • List node names only:
Best practices
  • Inspect -o json first to locate the exact path.
  • Build JSONPath expressions incrementally and test frequently.
  • Use custom-columns for simple, fixed-column tabular output.
  • For multi-line, row-oriented reports prefer range or custom-columns.
  • Quote JSONPath expressions with single quotes in the shell to avoid unwanted expansions.
If you are new to JSONPath, practice on sample kubectl -o json output. Iteratively build the path and test with kubectl -o jsonpath to reduce trial-and-error.
Links and references
  • Kubernetes Documentation
  • JSONPath reference and testers (search for “JSONPath evaluator” to find interactive tools)
  • KodeKloud learning platform for hands-on Kubernetes labs
That concludes this lesson. Try these commands against real cluster resources to reinforce the patterns and make them part of your debugging and reporting toolkit.

Watch Video

Practice Lab