<p>Im trying to use jq on kubernetes json output, to create new json object containing list of objects - container and image per pod, however im getting cartesian product.</p>
<p>my input data (truncated from sensitive info):</p>
<pre><code>{
    &quot;apiVersion&quot;: &quot;v1&quot;,
    &quot;items&quot;: [
        {
            &quot;apiVersion&quot;: &quot;v1&quot;,
            &quot;kind&quot;: &quot;Pod&quot;,
            &quot;metadata&quot;: {
                &quot;creationTimestamp&quot;: &quot;2021-06-30T12:45:40Z&quot;,
                &quot;name&quot;: &quot;pod-1&quot;,
                &quot;namespace&quot;: &quot;default&quot;,
                &quot;resourceVersion&quot;: &quot;757679286&quot;,
                &quot;selfLink&quot;: &quot;/api/v1/namespaces/default/pods/pod-1&quot;
            },
            &quot;spec&quot;: {
                &quot;containers&quot;: [
                    {
                        &quot;image&quot;: &quot;image-1&quot;,
                        &quot;imagePullPolicy&quot;: &quot;Always&quot;,
                        &quot;name&quot;: &quot;container-1&quot;,
                        &quot;resources&quot;: {},
                        &quot;terminationMessagePath&quot;: &quot;/dev/termination-log&quot;,
                        &quot;terminationMessagePolicy&quot;: &quot;File&quot;,
                        &quot;volumeMounts&quot;: [
                            {
                                &quot;mountPath&quot;: &quot;/var/run/secrets/kubernetes.io/serviceaccount&quot;,
                                &quot;readOnly&quot;: true
                            }
                        ]
                    },
                    {
                        &quot;image&quot;: &quot;image-2&quot;,
                        &quot;imagePullPolicy&quot;: &quot;Always&quot;,
                        &quot;name&quot;: &quot;container-2&quot;,
                        &quot;resources&quot;: {},
                        &quot;terminationMessagePath&quot;: &quot;/dev/termination-log&quot;,
                        &quot;terminationMessagePolicy&quot;: &quot;File&quot;,
                        &quot;volumeMounts&quot;: [
                            {
                                &quot;mountPath&quot;: &quot;/var/run/secrets/kubernetes.io/serviceaccount&quot;,
                                &quot;readOnly&quot;: true
                            }
                        ]
                    }
                ],
                &quot;dnsPolicy&quot;: &quot;ClusterFirst&quot;,
                &quot;enableServiceLinks&quot;: true,
                &quot;priority&quot;: 0,
                &quot;restartPolicy&quot;: &quot;Always&quot;,
                &quot;schedulerName&quot;: &quot;default-scheduler&quot;,
                &quot;securityContext&quot;: {},
                &quot;serviceAccount&quot;: &quot;default&quot;,
                &quot;serviceAccountName&quot;: &quot;default&quot;,
                &quot;terminationGracePeriodSeconds&quot;: 30,
                &quot;tolerations&quot;: [
                    {
                        &quot;effect&quot;: &quot;NoExecute&quot;,
                        &quot;key&quot;: &quot;node.kubernetes.io/not-ready&quot;,
                        &quot;operator&quot;: &quot;Exists&quot;,
                        &quot;tolerationSeconds&quot;: 300
                    },
                    {
                        &quot;effect&quot;: &quot;NoExecute&quot;,
                        &quot;key&quot;: &quot;node.kubernetes.io/unreachable&quot;,
                        &quot;operator&quot;: &quot;Exists&quot;,
                        &quot;tolerationSeconds&quot;: 300
                    }
                ],
                &quot;volumes&quot;: [
                    {
                        &quot;name&quot;: &quot;default-token-b954f&quot;,
                        &quot;secret&quot;: {
                            &quot;defaultMode&quot;: 420,
                            &quot;secretName&quot;: &quot;default-token-b954f&quot;
                        }
                    }
                ]
            },
            &quot;status&quot;: {
                &quot;conditions&quot;: [
                    {
                        &quot;lastProbeTime&quot;: null,
                        &quot;lastTransitionTime&quot;: &quot;2021-06-30T12:45:40Z&quot;,
                        &quot;status&quot;: &quot;True&quot;,
                        &quot;type&quot;: &quot;Initialized&quot;
                    },
                    {
                        &quot;lastProbeTime&quot;: null,
                        &quot;lastTransitionTime&quot;: &quot;2021-06-30T12:45:40Z&quot;,
                        &quot;message&quot;: &quot;containers with unready status: [container-1 container-2]&quot;,
                        &quot;reason&quot;: &quot;ContainersNotReady&quot;,
                        &quot;status&quot;: &quot;False&quot;,
                        &quot;type&quot;: &quot;Ready&quot;
                    },
                    {
                        &quot;lastProbeTime&quot;: null,
                        &quot;lastTransitionTime&quot;: &quot;2021-06-30T12:45:40Z&quot;,
                        &quot;message&quot;: &quot;containers with unready status: [container-1 container-2]&quot;,
                        &quot;reason&quot;: &quot;ContainersNotReady&quot;,
                        &quot;status&quot;: &quot;False&quot;,
                        &quot;type&quot;: &quot;ContainersReady&quot;
                    },
                    {
                        &quot;lastProbeTime&quot;: null,
                        &quot;lastTransitionTime&quot;: &quot;2021-06-30T12:45:40Z&quot;,
                        &quot;status&quot;: &quot;True&quot;,
                        &quot;type&quot;: &quot;PodScheduled&quot;
                    }
                ],
                &quot;containerStatuses&quot;: [
                    {
                        &quot;image&quot;: &quot;image-1&quot;,
                        &quot;imageID&quot;: &quot;&quot;,
                        &quot;lastState&quot;: {},
                        &quot;name&quot;: &quot;container-1&quot;,
                        &quot;ready&quot;: false,
                        &quot;restartCount&quot;: 0,
                        &quot;started&quot;: false,
                        &quot;state&quot;: {
                            &quot;waiting&quot;: {
                                &quot;message&quot;: &quot;Back-off pulling image \&quot;image-1\&quot;&quot;,
                                &quot;reason&quot;: &quot;ImagePullBackOff&quot;
                            }
                        }
                    },
                    {
                        &quot;image&quot;: &quot;image-2&quot;,
                        &quot;imageID&quot;: &quot;&quot;,
                        &quot;lastState&quot;: {},
                        &quot;name&quot;: &quot;container-2&quot;,
                        &quot;ready&quot;: false,
                        &quot;restartCount&quot;: 0,
                        &quot;started&quot;: false,
                        &quot;state&quot;: {
                            &quot;waiting&quot;: {
                                &quot;message&quot;: &quot;Back-off pulling image \&quot;image-2\&quot;&quot;,
                                &quot;reason&quot;: &quot;ImagePullBackOff&quot;
                            }
                        }
                    }
                ],
                &quot;qosClass&quot;: &quot;BestEffort&quot;,
                &quot;startTime&quot;: &quot;2021-06-30T12:45:40Z&quot;
            }
        }
    ],
    &quot;kind&quot;: &quot;List&quot;,
    &quot;metadata&quot;: {
        &quot;resourceVersion&quot;: &quot;&quot;,
        &quot;selfLink&quot;: &quot;&quot;
    }
}
</code></pre>
<p>my command:</p>
<pre><code>jq '.items[] | { &quot;name&quot;: .metadata.name, &quot;containers&quot;: [{ &quot;name&quot;: .spec.containers[].name, &quot;image&quot;: .spec.containers[].image }]} '
</code></pre>
<p>desired output:</p>
<pre><code>{
  &quot;name&quot;: &quot;pod_1&quot;,
  &quot;containers&quot;: [
    {
      &quot;name&quot;: &quot;container_1&quot;,
      &quot;image&quot;: &quot;image_1&quot;
    },
    {
      &quot;name&quot;: &quot;container_2&quot;,
      &quot;image&quot;: &quot;image_2&quot;
    }
  ]
}
</code></pre>
<p>output I get:</p>
<pre><code>{
  &quot;name&quot;: &quot;pod-1&quot;,
  &quot;containers&quot;: [
    {
      &quot;name&quot;: &quot;container-1&quot;,
      &quot;image&quot;: &quot;image-1&quot;
    },
    {
      &quot;name&quot;: &quot;container-1&quot;,
      &quot;image&quot;: &quot;image-2&quot;
    },
    {
      &quot;name&quot;: &quot;container-2&quot;,
      &quot;image&quot;: &quot;image-1&quot;
    },
    {
      &quot;name&quot;: &quot;container-2&quot;,
      &quot;image&quot;: &quot;image-2&quot;
    }
  ]
}
</code></pre>
<p>Could anyone explain what am I doing wrong?</p>
<p>Best Regards, Piotr.</p>

Zegal
<p>The problem is <code>&quot;name&quot;: .spec.containers[].name</code> and <code>&quot;image&quot;: .spec.containers[].image</code>:
Both expressions generate a sequence of each value for <code>name</code> and <code>image</code> which will than be combined.</p>
<p>Simplified example of why you get a Cartesian product:</p>
<p><code>jq -c -n '{name: (&quot;A&quot;, &quot;B&quot;), value: (&quot;C&quot;, &quot;D&quot;)}'</code></p>
<p>outputs:</p>
<pre class="lang-json prettyprint-override"><code>{&quot;name&quot;:&quot;A&quot;,&quot;value&quot;:&quot;C&quot;}
{&quot;name&quot;:&quot;A&quot;,&quot;value&quot;:&quot;D&quot;}
{&quot;name&quot;:&quot;B&quot;,&quot;value&quot;:&quot;C&quot;}
{&quot;name&quot;:&quot;B&quot;,&quot;value&quot;:&quot;D&quot;}
</code></pre>
<hr />
<p>You get the desired output using this <code>jq</code> filter on your input:</p>
<pre><code>jq '
  .items[]
  | {
      &quot;name&quot;: .metadata.name,
      &quot;containers&quot;: .spec.containers
                    | map({name, image})
    }'
</code></pre>
<p>output:</p>
<pre class="lang-json prettyprint-override"><code>{
  &quot;name&quot;: &quot;pod-1&quot;,
  &quot;containers&quot;: [
    {
      &quot;name&quot;: &quot;container-1&quot;,
      &quot;image&quot;: &quot;image-1&quot;
    },
    {
      &quot;name&quot;: &quot;container-2&quot;,
      &quot;image&quot;: &quot;image-2&quot;
    }
  ]
}
</code></pre>
