<pre><code>echo &quot;apiVersion: v1
kind: Node
metadata:
  name: host-cluster-control-plane-64j47
  labels:
    beta.kubernetes.io/arch: amd64
&quot; | yq -o p
</code></pre>
<p>Result:</p>
<pre><code>apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels.beta.kubernetes.io/arch = amd64
</code></pre>
<p>That's almost what I want. I am looking for the key to get values.</p>
<p>I could use <code>metadata.name</code> like this:</p>
<pre><code>echo &quot;apiVersion: v1
kind: Node
metadata:
  name: host-cluster-control-plane-64j47
  labels:
    beta.kubernetes.io/arch: amd64
&quot; | yq '.metadata.name'
</code></pre>
<p>But the <code>-o p</code> option of <code>yq</code> does not quote the key, if needed.</p>
<p>I can't use <code>metadata.labels.beta.kubernetes.io/arch</code> as key, since the correct syntax is <code>metadata.labels[&quot;beta.kubernetes.io/arch&quot;]</code>.</p>
<p>Is there an automated way to get the keys of a yaml file so that I can use the keys in <code>yq</code> (or <code>jq</code>)?</p>
<p>The desired output would be something like this:</p>
<pre><code>apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels[&quot;beta.kubernetes.io/arch&quot;] = amd64
</code></pre>
<p>I am looking for the valid key, because I want to create a second command line to select these values.</p>
<p>For example:</p>
<pre><code>❯ k get nodes -o yaml | yq '.items[].metadata.labels[&quot;beta.kubernetes.io/arch&quot;]'

amd64
amd64
amd64
</code></pre>

guettli
<p>You can get close by doing something like:</p>
<pre class="lang-bash prettyprint-override"><code>yq '(.. | key | select(test(&quot;\.&quot;))) |= (&quot;[\&quot;&quot; + . + &quot;\&quot;]&quot;)' file.yaml -op

apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels.[&quot;beta.kubernetes.io/arch&quot;] = amd64

</code></pre>
<p>Or you could do:</p>
<pre><code>yq '(.. | key | select(test(&quot;\.&quot;))) |= sub(&quot;\.&quot;, &quot;\.&quot;)' file.yaml -op

apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels.beta\\.kubernetes\\.io/arch = amd64
</code></pre>
<p>BTW - I'm not sure how it's supposed be escaped in property files, I'd be willing to update yq to do it natively someone raises a bug with details on github...</p>
<p>Disclaimer: I wrote yq</p>
