How to parse echo json value?

6/7/2019

I want to parse a value or convert a value into json format.

I have no idea how to do it.

echo -e $(kubectl get pods "test-pod" -o jsonpath="{range .status.containerStatuses[*]}{.state}"\\n"{end}")
map[running:map[startedAt:2019-06-07T00:51:34Z]]
map[running:map[startedAt:2019-06-07T00:51:40Z]]
map[running:map[startedAt:2019-06-07T00:51:44Z]]
map[waiting:map[message:Back-off 5m0s restarting failed container=con4 pod=test-pod_test(609c90e4-88be-11e9-ba5f-fa163e9a67be) reason:CrashLoopBackOff]]

I would like to get only all containers' status like [running, running, running, waiting].

Thanks in advance.

-- aspirant75
json
kubernetes
shell

1 Answer

6/7/2019

You can achieve it using the jq and keys[] command in jq. The following will be the command to use:

kubectl get pods kube-dns-86f4d74b45-khd4z -n kube-system -o json | jq -r '.status.containerStatuses[].state | keys[]'

The above command will give the following output of all container running or waiting or any state

-- Prafull Ladha
Source: StackOverflow