
- Target audience: Kubernetes administrators and engineers who need concise, scriptable views of Kubernetes resources.
- Outcome: be able to extract, format, and sort data from
kubectlJSON responses using JSONPath and built-in kubectl formatting options.
- 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.

- Extract precise fields from the raw JSON.
- Reformat data for scripts, reports, and monitoring checks.
- Create compact, human-friendly tabular views without external tooling.

- 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 jsonto inspect the full JSON returned by the API.
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
- Identify the kubectl command that fetches the resources you need (for example
kubectl get nodesorkubectl get pods --all-namespaces). - Inspect the raw JSON to discover the paths you need:
kubectl get <resource> -o json(or-o=json). - Craft a JSONPath expression that selects the needed fields. Test in a JSONPath evaluator if helpful.
- 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.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.- Pod container image — inspect JSON then extract a field
.items[0].spec.containers[0].image
Use it with kubectl:
- Node fields — basic JSONPath queries Wrap expressions in backticks when referenced in text. Examples that list values for all items:
{"\n"} and {"\t"}:
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}
-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:
--sort-by=<jsonpath> (use the same per-item path as in custom-columns):
Example — sort by name and print custom columns:
Best practices
- Always inspect
kubectl get <resource> -o jsonfirst to confirm JSON paths. - Prefer
-o=custom-columnsfor simple tabular reports. - Use
jsonpathwithrangefor complex or mixed fields. - Be mindful of shell quoting when embedding JSONPath expressions in scripts.
- Test expressions interactively before using them in automation.
- 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
rangeconstruct to produce per-item rows, or-o=custom-columnsfor simpler column definitions. - Use
--sort-byto control output order by a JSON field.
- 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
NODEandCPU.