13.01 JSON Path in Kubernetes
JSONPath in Kubernetes is used with kubectl to extract specific fields from large Kubernetes API outputs. It is useful for CKA tasks, production troubleshooting, reporting, and filtering large clusters with many Pods, Nodes, Deployments, ReplicaSets, Services, and Secrets.
Goal
Use JSONPath with kubectl to:
- inspect Kubernetes objects in JSON format
- extract specific fields
- print custom reports
- loop through lists
- create custom columns
- sort output using object fields
Why JSONPath?
In production clusters, you may deal with:
- hundreds of Nodes
- thousands of Pods
- many Deployments, ReplicaSets, Services, Secrets, ConfigMaps, and PVCs
Reading full YAML or JSON manually is slow. JSONPath helps you extract only the fields you need.
Use cases
- Print all Node names
- Print Node CPU capacity
- Print Pod images
- Print Node taints
- Print architecture of each Node
- Sort Pods or Nodes by a field
- Create custom table output for quick troubleshooting
Tip
Use JSONPath when built-in outputs like kubectl get, kubectl get -o wide, or kubectl describe do not show the field in the format you need.
How kubectl Output Works
kubectl talks to the Kubernetes API server. The API server returns data in JSON. By default, kubectl formats that JSON into a human-readable table.
Example output:
To see the raw JSON:
Note
JSONPath queries are written against the raw JSON structure, not against the default table output.
Basic JSONPath Workflow
Follow this method when building JSONPath commands.
1. Identify the kubectl command
2. View the output as JSON
3. Build the JSONPath query
4. Run kubectl with -o jsonpath
Warning
The JSONPath query should be wrapped in:
Example:
Important JSON Structure
Most kubectl get <resource> -o json outputs contain a top-level items list.
{
"apiVersion": "v1",
"items": [
{
"kind": "Node",
"metadata": {
"name": "master"
},
"status": {
"capacity": {
"cpu": "4"
}
}
}
],
"kind": "List"
}
Common paths:
| Field | JSONPath |
|---|---|
| First item name | .items[0].metadata.name |
| All item names | .items[*].metadata.name |
| First Pod first container image | .items[0].spec.containers[0].image |
| All Node CPU capacity | .items[*].status.capacity.cpu |
| All Node architecture | .items[*].status.nodeInfo.architecture |
Tip
Use [*] when you want all objects in a list. Use [0], [1], etc. when you want a specific item by index.
Basic JSONPath Examples
Print Node Names
Output:
Print Node Architecture
Output:
Print Node CPU Capacity
Output:
Print First Pod First Container Image
Output:
Formatting Output
By default, JSONPath output is usually printed on one line. Add newline and tab characters to make it readable.
| Format | Meaning |
|---|---|
{"\\n"} |
new line |
{"\\t"} |
tab |
Print Names and CPU on Separate Lines
Output:
Print Names and CPU on Same Line
Output:
Note
{"\\n"} and {"\\t"} are formatting strings inside the JSONPath expression.
Loops with range
Use range when you want clean row-by-row output.
Print Node and CPU Count
kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\\t"}{.status.capacity.cpu}{"\\n"}{end}'
Output:
Explanation:
{range .items[*]} for each item
{.metadata.name} print node name
{"\\t"} print tab
{.status.capacity.cpu} print CPU count
{"\\n"} print new line
{end} end loop
CKA-friendly pattern
Memorize this format:
JSONPath for Custom Columns
custom-columns is often easier than complex JSONPath loops when you want table output.
Syntax
Note
With custom-columns, do not include .items[*]. Kubernetes applies the path to each item automatically.
Node and CPU Custom Columns
Output:
Node and Architecture
Output:
Pod and Image
Output:
Success
Use custom-columns when you need clean output quickly during an exam.
JSONPath for Sorting
Use --sort-by to sort resources by a JSON field.
Sort Nodes by Name
Sort Nodes by CPU Capacity
Sort Pods by Name
Sort Pods by Creation Time
Tip
Sorting is useful when checking oldest/newest Pods, Nodes, or resources during troubleshooting.
Common Kubernetes JSONPath Queries
| Requirement | Command |
|---|---|
| Node names | kubectl get nodes -o=jsonpath='{.items[*].metadata.name}' |
| Node CPU | kubectl get nodes -o=jsonpath='{.items[*].status.capacity.cpu}' |
| Node architecture | kubectl get nodes -o=jsonpath='{.items[*].status.nodeInfo.architecture}' |
| Pod names | kubectl get pods -o=jsonpath='{.items[*].metadata.name}' |
| Pod IPs | kubectl get pods -o=jsonpath='{.items[*].status.podIP}' |
| Pod images | kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].image}' |
| Node internal IPs | kubectl get nodes -o=jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}' |
| Service ClusterIPs | kubectl get svc -o=jsonpath='{.items[*].spec.clusterIP}' |
| PVC storage requests | kubectl get pvc -o=jsonpath='{.items[*].spec.resources.requests.storage}' |
Useful Examples for CKA
Print Pod Name and Pod IP
Print Pod Name and Node Name
Print Node Name and Taints
Print Node Name and Internal IP
Print Namespace and Pod Name
Print Pod, Container, and Image
kubectl get pods -o=custom-columns=POD:.metadata.name,CONTAINER:.spec.containers[*].name,IMAGE:.spec.containers[*].image
Warning
If a Pod has multiple containers, [*] may print multiple values in the same cell. For exact formatting, use range.
JSONPath with Conditions
You can filter lists using conditions.
Get Internal Node IPs
Get External Node IPs
Note
Conditional filters are helpful, but in CKA tasks custom-columns and basic paths are usually enough.
JSONPath vs custom-columns vs sort-by
| Option | Best for | Example |
|---|---|---|
-o=jsonpath |
exact field extraction and formatting | print Pod images |
-o=custom-columns |
readable table reports | Node + CPU table |
--sort-by |
sorting normal kubectl output | sort Pods by age/name |
Production Best Practices
Do
- Use
-o jsonfirst to inspect the object structure. - Use
custom-columnsfor readable reports. - Use
rangefor clean row-by-row output. - Use
--sort-bywhen checking oldest/newest resources. - Save frequently used JSONPath commands as shell aliases or runbooks.
- Use JSONPath in troubleshooting scripts and audit checks.
Don't
- Do not guess paths without checking
-o json. - Do not use JSONPath when a simple
kubectl getoutput is enough. - Do not rely only on item indexes like
[0]in production scripts unless order is guaranteed. - Do not expose sensitive fields from Secrets in shared terminals or logs.
- Do not confuse container name with image name.
Common Mistakes
Mistake 1: Missing quotes
Wrong:
Better:
Mistake 2: Using items in custom-columns
Wrong:
Correct:
Mistake 3: Wrong list index
If you use:
you only get the first object. Use:
when you want all objects.
Quick Revision Cheat Sheet
# Raw JSON
kubectl get nodes -o json
kubectl get pods -o json
# All Node names
kubectl get nodes -o=jsonpath='{.items[*].metadata.name}'
# All Pod images
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].image}'
# Node name + CPU using range
kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\\t"}{.status.capacity.cpu}{"\\n"}{end}'
# Custom columns
kubectl get nodes -o=custom-columns=NODE:.metadata.name,CPU:.status.capacity.cpu
# Sort
kubectl get nodes --sort-by=.metadata.name
kubectl get pods --sort-by=.metadata.creationTimestamp
Exam Strategy
Quote
In CKA, JSONPath is mainly about speed. First inspect with -o json, then extract only what the question asks for.
Recommended approach:
1. Run kubectl get <resource> -o json
2. Find the field path
3. Test with -o=jsonpath
4. Use custom-columns if table output is required
5. Use --sort-by if ordering is required
Best CKA habit
When asked to find or print a specific field, avoid manually scanning kubectl describe. Use JSONPath or custom columns for faster and cleaner answers.