<p>I am trying to output the value for .metadata.name followed by the student's name in .spec.template.spec.containers[].students[] array using the regex test() function in jq.</p>
<p>I am having trouble to retrieve the individual array value since there is no key specified for the students[] array.</p>
<p>For example, if I check the students[] array if it contains the word &quot;Jeff&quot;, I would like the output to display as below:</p>
<pre><code>student-deployment:    Jefferson
</code></pre>
<p><strong>What i have tried:</strong></p>
<p>I've tried the command below which somewhat works but I am not sure how to get only the &quot;Jefferson&quot; value. The command below would print out all of the students[] array values which is not what I want. I am using Powershell to run the command below.</p>
<pre><code>kubectl get deployments -o json | jq -r '.items[] | select(.spec.template.spec.containers[].students[]?|test(&quot;\&quot;^Jeff.\&quot;&quot;)) | .metadata.name, &quot;\&quot;:\t\&quot;&quot;, .spec.template.spec.containers[].students'
</code></pre>
<p>Is there a way to print a specific value of an array given a condition in jq if there is no key specified? Also, would the solution work if there are multiple deployments?</p>
<p>The deployment template below is in json and I shortened it to only the relevant parts.</p>
<pre><code>{
    &quot;apiVersion&quot;: &quot;v1&quot;,
    &quot;items&quot;: [
        {
            &quot;apiVersion&quot;: &quot;apps/v1&quot;,
            &quot;kind&quot;: &quot;Deployment&quot;,
            &quot;metadata&quot;: {
                &quot;name&quot;: &quot;student-deployment&quot;,
                &quot;namespace&quot;: &quot;default&quot;
            },
            &quot;spec&quot;: {
                &quot;template&quot;: {
                    &quot;spec&quot;: {
                        &quot;containers&quot;: [
                            {
                                &quot;students&quot;: [
                                        &quot;Alice&quot;,
                                        &quot;Bob&quot;,
                                        &quot;Peter&quot;,
                                        &quot;Sally&quot;,
                                        &quot;Jefferson&quot;
                                ]
                            }
                        ]
                    }
                }
            }
        }
    ]
}
</code></pre>

calmmash
<p>For this approch, we introduce a variable <code>$pattern</code>. You may set it with <code>--arg pattern</code> to your regex, e.g. <code>&quot;Jeff&quot;</code> or <code>&quot;^Al&quot;</code> or <code>&quot;e$&quot;</code> to have the student list filtered by <code>test</code>, or leave it empty to see all students.</p>
<p>Now, we iterate over all <code>.item[]</code> elements (i.e. over &quot;all deployments&quot;). For each found, we output the content of <code>.metadata.name</code> followed by a literal colon and a space. Then we iterate again over all <code>.spec.template.spec.containers[].students[]</code>, perform the pattern <code>test</code> and concatenate the outcome.</p>
<p>To print out raw strings instead of JSON, we use the <code>-r</code> option when calling <code>jq</code>.</p>
<pre><code>kubectl get deployments -o json \
| jq --arg pattern &quot;Jeff&quot; -r '
    .items[]
    | .metadata.name + &quot;: &quot; + (
        .spec.template.spec.containers[].students[]
        | select(test($pattern))
      )
  '
</code></pre>
