
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

- 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.

- 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.
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
-
Identify the kubectl command to fetch the target resources:
- Nodes:
kubectl get nodes - Pods:
kubectl get pods - Deployments:
kubectl get deployments
- Nodes:
- Inspect the raw JSON:
- Examine the JSON structure and design the JSONPath expression that selects the desired field(s). Example to get the first pod container image:
- Apply the JSONPath query with kubectl:
- 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='{...}'.
Common JSONPath examples with kubectl
- Node names:
- Node architecture:
- Node CPU capacity:
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
- print
- end range
-o custom-columns is easier. Note: custom-columns applies the column paths to each item, so you omit the .items prefix.
Example:
--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:
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.- 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.