How to parse JSON format output of "kubectl get pods" using jq and create an array

11/2/2018

The JSON output returned to me after running this command

kubectl get pods -o json | jq '.items[].spec.containers[].env'

on my kuberntes cluster is this

[
  {
    "name": "USER_NAME",
    "value": "USER_NAME_VALUE_A"
  },
  {
    "name": "USER_ADDRESS",
    "value": "USER_ADDRESS_VALUE_A"
  }
]
[
  {
    "name": "USER_NAME",
    "value": "USER_NAME_VALUE_B"
  },
  {
    "name": "USER_ADDRESS",
    "value": "USER_ADDRESS_VALUE_B"
  }
]

I'd like to create a unified array/dictionary (Using Bash script) which looks like the example below and how can I get the value of each key?

[
  {
    "USER_NAME": "USER_NAME_VALUE_A",
    "USER_ADDRESS": "USER_ADDRESS_VALUE_A"
  },
  {
    "USER_NAME": "USER_NAME_VALUE_B",
    "USER_ADDRESS": "USER_ADDRESS_VALUE_B"
  }
]
-- James
jq
json
kubectl
kubernetes

2 Answers

11/2/2018

use the jsonpath

C02W84XMHTD5:~ iahmad$ kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}'
coredns-c4cffd6dc-nsd2k
etcd-minikube
kube-addon-manager-minikube
kube-apiserver-minikube
kube-controller-manager-minikube
kube-dns-86f4d74b45-d5njm
kube-proxy-pg89s
kube-scheduler-minikube
kubernetes-dashboard-6f4cfc5d87-b7n7v
storage-provisioner
tiller-deploy-778f674bf5-vt4mj

https://kubernetes.io/docs/reference/kubectl/jsonpath/

it can output key values as well

C02W84XMHTD5:~ iahmad$ kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
coredns-c4cffd6dc-nsd2k 2018-10-16T21:44:19Z
etcd-minikube   2018-10-29T17:30:56Z
kube-addon-manager-minikube 2018-10-29T17:30:56Z
kube-apiserver-minikube 2018-10-29T17:30:56Z
kube-controller-manager-minikube    2018-10-29T17:30:56Z
kube-dns-86f4d74b45-d5njm   2018-10-16T21:44:16Z
kube-proxy-pg89s    2018-10-29T17:32:05Z
kube-scheduler-minikube 2018-10-29T17:30:56Z
kubernetes-dashboard-6f4cfc5d87-b7n7v   2018-10-16T21:44:19Z
storage-provisioner 2018-10-16T21:44:19Z
tiller-deploy-778f674bf5-vt4mj  2018-11-01T13:45:23Z

then you can split those by space and form json or list

-- Ijaz Ahmad Khan
Source: StackOverflow

11/2/2018

This will do it in bash. You'd be surprised how much you can do with bash:

#!/bin/bash

NAMES=`kubectl get pods -o=jsonpath='{range .items[*]}{.spec.containers[*].env[*].name}{"\n"}' | tr -d '\011\012\015'`
VALUES=`kubectl get pods -o=jsonpath='{range .items[*]}{.spec.containers[*].env[*].value}{"\n"}' | tr -d '\011\012\015'`

IFS=' ' read -ra NAMESA <<< "$NAMES"
IFS=' ' read -ra VALUESA <<< "$VALUES"

MAXINDEX=`expr ${#NAMESA[@]} - 1`

printf "[\n"
for i in "${!NAMESA[@]}"; do
  printf "  {\n"
  printf "  \"USER_NAME\": \"${NAMESA[$i]}\",\n"
  printf "  \"USER_ADDRESS\": \"${VALUESA[$i]}\"\n"
  if [ "$i" == "${MAXINDEX}" ]; then
    printf "  }\n"
  else
    printf "  },\n"
  fi
done
printf "]\n"
-- Rico
Source: StackOverflow