<p>I want to filter namespaces based on the annotation. I do not want namespace which has annotation as <code>components\.gke\.io/layer==&quot;addon&quot;</code>.</p>
<p>I tried <code>kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer==&quot;addon&quot;)].metadata.name}'</code> command and at least it gives me the result but it is not coming in normal table format, the way it comes when you execute <code>kubectl get ns</code>.</p>
<p><a href="https://i.stack.imgur.com/Oe5YW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Oe5YW.png" alt="enter image description here" /></a></p>
<p>However, this is not what I want, I want the inverse of this, which means filtering out these GKE rows. I tried <code>kubectl get ns -o=jsonpath='{.items[?(@.metadata.annotations.components\.gke\.io/layer==&quot;&quot;)].metadata.name}'</code> but it doesn't work, it give empty result.</p>
<p>I tried to format but unable to create the desired command. Anyone has tried this before - (1.) filter out namespaces which has certain annotation (2.) format that output in table format.</p>

<p>I think (!?) you can't use negation with <code>kubectl</code>'s implementation of <a href="https://kubernetes.io/docs/reference/kubectl/jsonpath/" rel="nofollow noreferrer">JSONPath</a>.</p>
<p>Once you use the <code>--output</code> flag with <code>kubectl</code>, you lose the ability to render the command's output using the default table format. With e.g. Bash <a href="https://man7.org/linux/man-pages/man1/column.1.html" rel="nofollow noreferrer"><code>column</code></a> can provide similar out.</p>
<p>Consider using a generic JSON processing tool  such as <a href="https://stedolan.github.io/jq/" rel="nofollow noreferrer"><code>jq</code></a>.</p>
<p>An advantage of using a standalone tool is that, once you're familiar with the tool, you can use it with any command that emits JSON (not just with <code>kubectl</code>.</p>
<pre class="lang-bash prettyprint-override"><code>FILTER='
  [&quot;NAME&quot;,&quot;STATUS&quot;],
(.items[]| 
  [
    select(.metadata.annotations[&quot;components.gke.io/layer&quot;]!=&quot;addon&quot;)
    |.metadata.name,.status.phase
  ]
)
|@tsv'

kubectl get namespaces --output=json \
| jq -r &quot;${FILTER}&quot; \
| column --table --separator $'\t'
</code></pre>
<p>Explanation of <code>FILTER</code>:</p>
<ol>
<li>Returns a JSON array that is piped into <code>@tsv</code> (tab-formatted output)</li>
<li>The first array entry is a an array (!) of column titles</li>
<li><code>.items</code> is piped through a <code>select</code> that filters Namespaces that don't contain the <code>annotation</code></li>
<li>Emit not filtered Namespace name and status as JSON array items</li>
</ol>
<p>The tab-formatted (no longer JSON) that's emitted by <code>jq</code> is then piped into <code>column</code> which formats the results as a table.</p>
