Kubernetes JSONPath
162 words
One minute
What is JSONPath?
JSONPath Expressions
JSONPath | Description |
---|
$ | The root object/element |
@ | The current object/element |
. or [] | Child operator |
.. | Recursive descent. |
* | Wildcard. All objects/elements regardless their names. |
[] | Subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator. |
[,] | Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
[start:end:step] | Array slice operator |
?() | Applies a filter (script) expression. |
() | Script expression, using the underlying script engine. |
Examples
1
| kubectl get nodes -o json > /opt/outputs/nodes.json
|
1
| kubectl get nodes node01 -o json > /opt/outputs/node01.json
|
1
| kubectl get nodes -o=jsonpath='{$.items[*].metadata.name}'
|
1
| kubectl get nodes -o=jsonpath='{$.items[*].status.nodeInfo.osImage}'
|
1
| kubectl config view --kubeconfig=/root/my-kube-config -o=jsonpath='{$.users[*].name}'
|
1
| kubectl get pv --sort-by='{.spec.capacity.storage}'
|
1
| kubectl get pv --sort-by=.spec.capacity.storage -o=custom-columns=NAME:.metadata.name,CAPACITY:.spec.capacity.storage
|
1
| kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.contexts[?(@.context.user=='aws-user')].name}"
|