How to parse the json to retrieve a field from output of
kubectl get pods -o jsonFrom the command line I need to obtain the system generated container name from a google cloud cluster ... Here are the salient bits of json output from above command : 
click here to see entire json output
So the top most json key is an array : items[] followed by metadata.labels.name where the search critera value of that compound key is "web" (see above image green marks). On a match, I then need to retrieve field
.items[].metadata.name which so happens to have value :
web-controller-5e6ij // I need to retrieve this valueI want to avoid text parsing output of
kubectl get podswhich is
NAME READY STATUS RESTARTS AGE
mongo-controller-h714w 1/1 Running 0 12m
web-controller-5e6ij 1/1 Running 0 9mFollowing will correctly parse this get pods command yet I feel its too fragile
kubectl get pods | tail -1 | cut -d' ' -f1After much battling this one liner does retrieve the container name :
kubectl get pods -o=jsonpath='{.items[?(@.metadata.labels.name=="web")].metadata.name}'when this is the known search criteria :
items[].metadata.labels.name == "web"and this is the desired field to retrieve
items[].metadata.name : "web-controller-5e6ij"If you want to filter by labels. You could just use kubectl -l flag. The following will do the same:
kubectl get pods -l name=web -o=jsonpath='{.items..metadata.name}'