Labs – JSON PATH – Kubernetes

Labs – JSON PATH – Kubernetes

part of k8status.json
“containerStatuses”: [
{
“image”: “nginx:alpine”,
“name”: “nginx”,
“ready”: false,
“restartCount”: 4,
“state”: {
“waiting”: {
“reason”: “ContainerCreating”
}
}
},
{
“image”: “redis:alpine”,
“name”: “redis-container”,
“ready”: false,
“restartCount”: 2,
“state”: {
“waiting”: {
“reason”: “ContainerCreating”
}
}
}
],

cat k8status.json | jpath $.status.containerStatuses[?( @.restartCount == 2)].name # this works
but below queries return empty []

cat k8status.json | jpath $.status.containerStatuses[?(@.name == ‘nginx’)].restartCount. # should return me 4.
cat k8status.json | jpath $.status.containerStatuses[?(@.name == ‘redis-container’)].restartCount. # should return me 2

what am i missing?

One thing you’re missing is the use of a

code block (use the </> button!!)
   - prevents " and ' characters from getting corrupted
   - is *much* easier to read.
   - preserves indentation

Beyond that: the top level field in your json looks to be “containerStatuses”; your jpath expression expects “status”. This alone would screw you up.

Thanks! I tried various combinations.

cat k8status.json | jpath ‘$.status.containerStatuses[?(@.name == “nginx”)].restartCount’. # worked finally.
cat k8status.json | jpath ‘$.status.containerStatuses[?(@.name == “redis-container”)].restartCount’. # worked finally.

whole jpath in single quote and text in double quote.
status is is there in actual json file , I just pasted the array as it would take up a lot of space.