Filer an array of objects with yq
Sophia Terry
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.yamlThat 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.yamlBut 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.yamlThinking 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.yamlBut 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