<p>The JSON output returned to me after running this command </p>

<pre><code>kubectl get pods -o json | jq '.items[].spec.containers[].env'
</code></pre>

<p>on my kuberntes cluster is this</p>

<pre><code>[
  {
    "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"
  }
]
</code></pre>

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

<pre><code>[
  {
    "USER_NAME": "USER_NAME_VALUE_A",
    "USER_ADDRESS": "USER_ADDRESS_VALUE_A"
  },
  {
    "USER_NAME": "USER_NAME_VALUE_B",
    "USER_ADDRESS": "USER_ADDRESS_VALUE_B"
  }
]
</code></pre>

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

<pre><code>#!/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 &lt;&lt;&lt; "$NAMES"
IFS=' ' read -ra VALUESA &lt;&lt;&lt; "$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"
</code></pre>
