Kubectl get JSONPath Support
External
Internal
Overview
kubectl get -o jsonpath='{<jsonpath-expression>}' ...
supports a JSONPath template, which consists in JSONPath expressions enclosed by curly braces.
kubectl get pod some-pod -o jsonpath='{.status.phase}'
More than one JSONPath expressions can be concatenated in a JSON template:
kubectl get pod some-pod -o jsonpath='the pod name {.metadata.name} and phase {.status.phase}'
The JSONPath expression are mainly used to:
- Filter the JSON content to only allow specific fields in the JSON object, using filter expressions.
- Format the output.
In addition to the original JSONPath syntax, kubectl provides the following extensions:
- The possibility to use double quotes to quote text inside JSONPath expressions.
- The availability of the range and end operators to iterate lists.
Note that when the result of the kubectl query contains more than one item, it is returned as a "List" top-level JSON Object, where individual results are element of an ".items" array:
{
"apiVersion": "v1",
"kind": "List",
"metadata": {
"resourceVersion": "..."
...
}
"items": [
{
"apiVersion": "v1",
"kind": "Namespace",
...
},
...
]
}
The result should be processed as such:
kubectl get pods -o jsonpath="{.items[*]...}"
Syntax
$
Element Selection
Object Fields Selection
Individual Fields
The value of a specified field can be obtained with:
kubectl get pod <pod-name> -o jsonpath="{.status.phase}"
When more than one resource is returned as result, the result is a JSON "List" Object with an .items field, which can be queried as such:
kubectl get pods -o jsonpath="{.items[*].status.phase}"
Field Recursive Descent
All values for a specific field can be obtained recursively with:
Array Element Selection
- JSONPath [0-based-index]
- JSONPath [index1, index2, ...]
- JSONPath [start:end], [start:]
- JSONPath [:n]
- JSONPath [-n:]
Wildcard
@
Filter Expressions
[?(expression)]
Organizatorium
Removing Leading and Trailing Single Quotes
... | sed -e 's/^'\''//' > ...
Get an Individual Attribute Only
TODO: https://gist.github.com/so0k/42313dbb3b547a0f51a547bb968696ba
kubectl ... -o jsonpath="{.items[?(@.spec.unschedulable)].metadata.name}"
Alternative, to explore and document:
kubectl get pods --no-headers -o custom-columns=\":metadata.name\" ...
Filter Elements of an Array based on a Key Value
We assume that the elements of the array are maps, which contain the specified key:
kubectl ... -o jsonpath="{.users[?(@.name=="blue")].user.password}" kubectl get pod ... -o jsonpath='{.items[0].spec.volumes[?(@.name=="vault")].hostPath.path}' 2>/dev/null
Select and Combine Two or More Elements
kubectl get pod \
-o jsonpath='{.items[0].spec.volumes[?(@.name=="A")].hostPath.path}'+'{.items[0].spec.volumes[?(@.name=="B")].hostPath.path}'
returns "/some/path/a+/some/path/b"
Same Element from Multiple Resources
kubectl get pod -o jsonpath='{.items[*].metadata.name}'
Array Length
TODO
kubectl get pods -o=jsonpath='{.items[?(@.metadata.labels.name=="web")].metadata.name}'
Iterate over the Elements of an Array
kubectl get nodes -o jsonpath='{.items[*]}'
Iterate over the Elements of an Array and Select a Specific Key
kubectl get nodes -o jsonpath='{.items[*].status}'
Filter by an element:
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")]}'
Print the element "address"
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'