Filer an Array of Objects with Yq

I have a yaml file that looks like this:

apiVersion: v1
entries:
  blue-green-toggle:
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.17
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.16
    apiVersion: v2
  - description: Used to toggle an application between blue and green
    name: blue-green-toggle
    version: 1.0.15
    apiVersion: v2
  istio-config:
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.4
    apiVersion: v2
  - description: Used to configure the cluster level settings of istio.
    name: istio-config
    version: 1.0.1
    apiVersion: v2
  latest-toggle:
    name: latest-toggle
    version: 1.0.5
    apiVersion: v2
  standard-helm-chart:
    name: standard-helm-chart
    version: 1.1.10
    apiVersion: v2
    name: standard-helm-chart
    version: 2.0.1
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.34
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.10
    apiVersion: v2
    name: standard-helm-chart
    version: 1.0.9
    apiVersion: v2
generated: 2021-06-22T00:22:33.1554922Z
...

I am trying to list the the version numbers that start with 1.0. and are listed in the standard-helm-chart section.

I have used this so far to get just the entries for the standard-helm-chart:

yq eval '.entries | .standard-arup-helm-chart' index.yaml

That worked fine. So then I then tried to get just to the rows with version matching 1.0.*. I read over the select documentation for yq, but it does not indicate how to match when you are looking at an object instead of just a string.

I tried this:

yq eval '.entries | .standard-arup-helm-chart | select(. == "1.0.*")' index.yaml

But this fails. And I would expect it to, because it can't compare the string of "1.0.*" to the whole object.

I also tried:

yq eval '.entries | .standard-arup-helm-chart | select(.version == "1.0.*")' index.yaml

Thinking that would let yq know that I want to just look at the version. But it said Error: Cannot index array with 'version'

I then figured I needed to try an array style syntax:

yq eval '.entries | .standard-arup-helm-chart | select(.[version] == "1.0.*")' index.yaml

But that fails with a parsing error.

What command can I send yq to get all the version numbers that start with 1.0.?

1 Answer

Took some trial an error, but this is what I ended up with:

yq eval '.entries.standard-helm-chart.[] | select(.version == "1.0.*") | .version' index.yaml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest