Shadi Kattaa:
hey people … any tricks for jsonpath tasks ? even if I know what to do , it needs a lot of time to find what you are looking for , and this may hart killing in the exam … any ideas or tips ?
Raamkanna Saranathan:
i do -o yaml first then traverse through that for jsonpath. it’s easier because of the indentation. -o json is hard to read.
Shadi Kattaa:
make sense
Daz Mac:
Look at the 2nd and 3rd from bottom examples in this section.
Here is the pod command
kubectl get pods -o json | jq -c 'path(..)|[.[]|tostring]|join(".")'
This will produce a list of ALL the keys in a pod i.e. .spec.containers.image etc…
You can then use the key in your jsonpath i.e. kubectl get pod -o jsonpath='{<KEY>}'
No need to memorise jsonpath. Also bear in mind that it is the output which is important not how you get - use jsonpath, grep, awk whatever as long as the output is in the format required.
Shadi Kattaa:
thank you very much!! this is really useful @Daz Mac
Daz Mac:
You can replace pod with any resource and get the list of keys so for example deployments
kubectl get *deployments* -o json | jq -c 'path(..)|[.[]|tostring]|join(".")
Shivesh:
Even I am interested to know - After doing lot of practice I am comfortable with YAML now. Do I still need to prepare on JASON understanding ?
Daz Mac:
@Shivesh in the exam you might be asked to output information into a file in a specific format.
So for example you might be asked to output container names and image (seperated by space) from all pods with label ‘x’ with one line per entry
nginx nginx:1.19 busybox busybox:1.28
How you do that - jsonpath, grep, awk or typing it in manually is irrelevant. The exam is checking the contents is accurate not how you got it. Sometimes it is easier to get it in the output format using jsonpath. However if you know what the output needs to be then sometimes it is easy to create the file rather than mess around trying to figure out how to get the data using jsonpath.
Shivesh:
Thanks Daz, I will practice to understand jasonpath as well. Regards