Skip to main content
In this lesson you’ll learn how to apply JSONPath expressions to kubectl output. You’ll see why JSONPath is useful for filtering and formatting Kubernetes API JSON, how kubectl exposes JSON, step-by-step guidance to craft JSONPath queries, practical examples (including loops), and how to create custom columns and sort results. At the end there are suggested practice exercises to reinforce what you learned.
A presentation slide titled "Objectives" with a dark blue left panel. To the right are teal numbered markers (06–09) listing topics: "Loops – Range," "Custom columns," "Sort," and "Practice tests and exercises."
Getting started
  • Target audience: Kubernetes administrators and engineers who need concise, scriptable views of Kubernetes resources.
  • Outcome: be able to extract, format, and sort data from kubectl JSON responses using JSONPath and built-in kubectl formatting options.
Prerequisites
  • Basic familiarity with JSONPath expressions. If you need a refresher, take a short JSONPath tutorial (for example the JSON Path Test - Free Course).
  • Practice working with raw JSON from Kubernetes before applying expressions in scripts.
A presentation slide titled "Prerequisite" on a dark blue background showing three rounded white buttons listing JSON Path resources: "JSON Path for Beginners on YouTube", "JSON Path Practice Tests on KodeKloud", and "JSON Path Practice Tests on Kubernetes Dataset on KodeKloud."
Why use JSONPath with kubectl? When managing production clusters you often need to inspect hundreds or thousands of objects (Nodes, Pods, Deployments, ReplicaSets, Services, Secrets, etc.). kubectl fetches resource data from the API server as JSON, but the default table output hides many useful fields. JSONPath allows you to:
  • Extract precise fields from the raw JSON.
  • Reformat data for scripts, reports, and monitoring checks.
  • Create compact, human-friendly tabular views without external tooling.
A presentation slide titled "Why JSON Path?" on a dark blue background. A rounded box lists reasons about large datasets, e.g., "100s of Nodes" and "1000s of PODs, Deployments, ReplicaSets."
How kubectl and the API server interact
  • kubectl is a client that queries the Kubernetes API server.
  • The API server returns resource data as JSON documents.
  • kubectl converts that JSON into a default human-readable table (hiding many fields).
  • Use -o json to inspect the full JSON returned by the API.
Example — default table vs full JSON Default table output:
Full JSON (trimmed for clarity):
If you need more fields in kubectl get output, -o wide adds a few columns but still omits many JSON fields. Use kubectl describe for a human-friendly, detailed view, or JSONPath to precisely extract the fields you need. Common report targets Here are typical fields you might want to extract with JSONPath: Four-step workflow to use JSONPath with kubectl
  1. Identify the kubectl command that fetches the resources you need (for example kubectl get nodes or kubectl get pods --all-namespaces).
  2. Inspect the raw JSON to discover the paths you need: kubectl get <resource> -o json (or -o=json).
  3. Craft a JSONPath expression that selects the needed fields. Test in a JSONPath evaluator if helpful.
  4. Apply the expression with kubectl: kubectl get <resource> -o jsonpath='<expression>'.
If you’re new to JSONPath, copy kubectl’s -o json output into a JSONPath evaluator (for example https://jsonpath.com) to experiment before using the expression in scripts.
Important: shell quoting and escaping Use single quotes around the jsonpath expression to avoid shell interpolation. If your expression contains single quotes, you may need to adjust quoting. Also, when inserting literal newline or tab characters in output, kubectl accepts {"\n"} and {"\t"} inside the JSONPath string.
Careful with shell quoting. Prefer single quotes around -o jsonpath='...'. If you must use double quotes, escape internal $ or backticks. Incorrect quoting is a common cause of errors.
Examples: examine JSON and create queries
  1. Pod container image — inspect JSON then extract a field
Trimmed Pod JSON (example):
JSONPath to the container image (example for the first item and first container): .items[0].spec.containers[0].image Use it with kubectl:
  1. Node fields — basic JSONPath queries Wrap expressions in backticks when referenced in text. Examples that list values for all items:
Formatting output: newline and tab To create multi-line or columned output, insert literal newline and tab characters inside the JSONPath string using {"\n"} and {"\t"}:
Loops with range — clean column output To produce tidy per-item rows, iterate over .items using the range construct. The pattern:
  • Start: {range .items[*]}
  • Inside: prepend fields with . (e.g., {.metadata.name})
  • Use {"\t"} and {"\n"} for tabs and newlines
  • Close: {end}
Example — print Name and CPU in columns:
Custom columns (-o=custom-columns) -o=custom-columns provides a simpler way to generate columns without manual JSONPath loops. Column definitions use COLUMN_NAME:JSONPath pairs, and the JSONPath is implicitly relative to each item (so omit .items[*]). Example:
Sorting results with —sort-by Sort the output by a per-item JSON field using --sort-by=<jsonpath> (use the same per-item path as in custom-columns): Example — sort by name and print custom columns:
Quick reference table — kubectl output options Best practices
  • Always inspect kubectl get <resource> -o json first to confirm JSON paths.
  • Prefer -o=custom-columns for simple tabular reports.
  • Use jsonpath with range for complex or mixed fields.
  • Be mindful of shell quoting when embedding JSONPath expressions in scripts.
  • Test expressions interactively before using them in automation.
Summary
  • kubectl gets data from the API server as JSON; the default table hides many fields.
  • JSONPath lets you extract and format those fields for reporting and automation.
  • Use the range construct to produce per-item rows, or -o=custom-columns for simpler column definitions.
  • Use --sort-by to control output order by a JSON field.
Practice exercises
  • Extract all pod names and their images across a namespace.
  • Create a custom-columns report that shows Pod, Node, and RestartCounts.
  • Sort nodes by CPU capacity and display NODE and CPU.
That’s it for this lesson — try the practice exercises to apply what you’ve learned and solidify your JSONPath skills with kubectl.

Watch Video

Practice Lab