<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 in JsonPath for a Kubectl query.</p>
<p>I had actually asked a similar question linked here for this in jq.</p>
<p><a href="https://stackoverflow.com/questions/69260360/how-do-i-print-a-specific-value-of-an-array-given-a-condition-in-jq-if-there-is/69260669?noredirect=1#comment122445926_69260669">How do I print a specific value of an array given a condition in jq if there is no key specified</a></p>
<p>The solution worked but I am wondering if there is an alternative solution for it using JsonPath or go-template perhaps (without the need for using jq).</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>
<p>student-deployment:    Jefferson</p>
<p><strong>What I've tried:</strong></p>
<p>For JsonPath, I've tried the query below:</p>
<pre><code>kubectl get deployment -o=jsonpath=&quot;{range .items[?(@.spec.template.spec.containers[*].students[*])]}{'\n'}{.metadata.name}{':\t'}{range .spec.template.spec.containers[*]}{.students[?(@==&quot;Jefferson&quot;)]}{end}{end}&quot;
</code></pre>
<p>But this only works to evaluate for matching words. Would it be possible to use JsonPath to query for regex as I've read here that JsonPath regex =~ doesn't work? I did try to use | grep and findstr but it still returned all values inside the array back to me. Other than jq, is there another way to retrieve the regex output?</p>
<p><a href="https://github.com/kubernetes/kubernetes/issues/61406" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/61406</a></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>

<p>The documentation for JSONPath Support clearly describes that it is not possible with JSONPath and you can use jq.</p>
<p><a href="https://kubernetes.io/docs/reference/kubectl/jsonpath/" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/kubectl/jsonpath/</a></p>
<p><a href="https://i.stack.imgur.com/VKr41.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VKr41.png" alt="enter image description here" /></a></p>
