Hi folks, I know, this is not very CKA specific but during my CKA course there w . . .

Olli D:
Hi folks,
I know, this is not very CKA specific but during my CKA course there was one section about how to find s jsonpath the easy way (at least I guess it was here …).

so having a JSON object like

{
“foo”: [
“bar”: {
“name”: “hello”
},
“baz”: {
“name”: “world”
}
]
}

with some magic I can get something like

.foo[0].bar.name: hello
.foo[1].baz.name: world

This would be useful when having large objects e.g. using grep on it to get the full path with one command.

I found some jq magic but this was rather a lot to type and I am sure, ther was some magic --path (or similar) switch so some cli command to get this full key path with its value.

jq magic is here: https://unix.stackexchange.com/questions/561460/how-to-print-path-and-key-values-of-json-file

Anybody an idea, what I am looking for and where to find it?

mjv:
something like .foo[0].bar?

Olli D:
yes - I accidentally pressed enter too early. See my full question above.

mjv:
if you wanna search for name values you could go with ..name search

Olli D:
Right but when it comes to large JSON objects with overlapping key names, it would be nice to get the whole (absolute) path to be sure to select the right attribute

Leo Pastor:
@Olli D something related, which is used for CKA - kubectl - since mentioned “path” (nodes is just an example of resource below):
kubectl get nodes -o json | jq -c paths

Leo Pastor:
Will see if I find something to add the value at the end. I do not remember of seeing it before, it looks interesting.

Olli D:
@Leo Pastor that looks like the command I was looking for. At least it delivers the absolute path to each element (unfortunately without value, but this boils it down to 2 commands: first grep for the value or scan the JSON an search the key, and then grep in jq -c paths for the absolute key path e.g.


“schedulerName”: “default-scheduler”,

$ kubectl get deploy -o json | jq -c paths | grep schedulerName
[“items”,0,“spec”,“template”,“spec”,“schedulerName”]
[“items”,1,“spec”,“template”,“spec”,“schedulerName”]
[“items”,2,“spec”,“template”,“spec”,“schedulerName”]
[“items”,3,“spec”,“template”,“spec”,“schedulerName”]

$ kubectl get deploy -o custom-columns=NAME:.metadata.name,SCHEDULER:.spec.template.spec.schedulerName

I know, if relative key is unique, I could just type …schedulerName - this was just a quick example. I some cases keys are not unique.