First example:
yq '.[] | select(.[] == "c")' examples/data1.yaml
Explanation:
- expand the top level entries
.[] - select the ones that have a (direct) child == "foo":
select(.[] == "c")
second example:
yq '.[] | select(.. == "foo")' examples/data1.yaml
Explanation:
- expand the top level entries
.[] - select the ones that have a any child == "foo":
select(.. == "c")(recursively checks the tree)
Not sure which output is desired - this is outputting the child values of the nodes. You could pipe it through 'key' to get the key node...
yq
mikefarah.gitbook.io › yq › operators › select
Select | yq - GitBook
Select is used to filter arrays and maps by a boolean expression · equals / not equals (==, !=) operators here
yq
mikefarah.gitbook.io › yq › operators › contains
Contains - yq - GitBook
March 18, 2023 - This means an expression like contains(["cat"]) will return true for an array ["cats"]. See the "Array has a subset array" example below on how to check for a subset. Array is equal or subset of · Given a sample.yml file of: Copy · - foobar - foobaz - blarp · then · Copy · yq 'contains(["baz", "bar"])' sample.yml ·
Extract all entries that contain value in array
Given an array: foo: - b - c bar: - 2 - c non: - a - b How do I extract all entries that contain "c", i.e. the entries "foo" and "bar". What if these entries were nest... More on github.com
conditional statements - How to select all YAML keys based on a subkey value using yq? - Stack Overflow
The trick is to convert the object ... the selection to the array elements and then convert it back into an object. The filter with_entries does these conversions under the hood. It is a shorthand for to_entries | map(foo) | from_entries (see jq manual). The call is slightly different depending on the yq implementation used: yq - Python or yq - Go. The bash script contains calls for ... More on stackoverflow.com
YQ - how to filter an array by field value if matching at least one element in a list - Stack Overflow
Specifically (https://github.com/mikefarah/yq) 4.35+ I have a comma-separated string of terms and I want to filter down a list if a field value is one of those terms. Ex) Given the following input: - More on stackoverflow.com
linux - How can i check if specific value exist in list with yq - Stack Overflow
How can i check if specific value exist in list with yq . This is my yaml projects: p1: - "sample/project/app1" - "sample/project/app2" This is my test code but it More on stackoverflow.com
Stack Overflow
stackoverflow.com › questions › 73551977 › how-to-yq-select-contains-on-a-field-with-an-object-with-a-value-of-tags-and-spe
aws cloudformation - How to yq select contains on a field with an object with a value of tags and special characters? - Stack Overflow
Partially working in that it always adds the policy creating duplicates, add_policy.yq: ( .Resources[] |= select( (.Properties.AssumeRolePolicyDocument.Statement[].Principal.Service.[] == "ecs-tasks.amazonaws.com" or .Properties.AssumeRolePolicyDocument.Statement[].Principal.Service.[] == "lambda.amazonaws.com") and (.Properties.ManagedPolicyArns | contains([{"Fn::ImportValue": "${MyPrefix}-my-policy-arn"}]) | not ) ) .Properties.ManagedPolicyArns += {"Fn::ImportValue": "${MyPrefix}-my-policy-arn" | .
GitHub
github.com › mikefarah › yq › discussions › 1356
Use ```or``` with ```contains``` · mikefarah/yq · Discussion #1356
September 22, 2022 - I want to select all entries in the primary array that contain in the tag array either the value "a" or "h" (or both). How do I go about this? Beta Was this translation helpful? Give feedback. 1 · You must be logged in to vote · All reactions · Comment options · There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. Quote reply · - Not sure what you mean by primary array, but: yq '.[] | select( (.irrelevant | contains(["h"]) or (.irrelevant | contains(["a"])) ))' file.yaml ·
Author mikefarah
cheat.sh
cheat.sh › yq
cheat.sh/yq
For version 4, use 'yq_v4' # Read spec.template node from example.yml yq r example.yml spec.template # Read from stdin cat sample.yaml | yq r - b.c # Print the path yq r --printMode p "a.thing*.*" # Print the path and value yq r --printMode pv "a.thing*.*" # Print the length of a list yq r sample.yml --length animals # Read with conditions yq r sample.yml spec.metadata[name==myapp] # Collect results into an array yq r sample.yaml --collect a.*.animal # Read from the 2nd document yq r -d1 sample.yaml b.c # Validate a document
yq
mikefarah.gitbook.io › yq › operators
Operators | yq - GitBook
yq · How It Works · Recipes · Upgrading from V3 · Commands · Evaluate · Evaluate All · Shell Completion · Operators · Add · Alternative (Default value) Anchor and Alias Operators · Array to Map · Assign (Update) Boolean Operators · Collect into Array · Column · Comment Operators · Compare Operators · Contains ...
HackingNote
hackingnote.com › en › cheatsheets › jq
Cheatsheet - jq
$ jq -r '.tags | .[] | select(contains("1.0.0")) | length'
Top answer 1 of 2
6
Please specify which implementation of yq you are using.
With mikefarah/yq, you could use any_c:
yq '.projects.p1 | any_c(. == "sample/project/app1")' test.yaml
With kislyuk/yq, you could use IN:
yq 'IN(.projects.p1[]; "sample/project/app1")' test.yaml
2 of 2
2
You cannot check whether array contains a string with has(). Please try instead:
yq < test.yaml '.projects.p1 | index("sample/project/app1") != null'
true
GitHub
github.com › mikefarah › yq › issues › 878
No way to match a pattern that contains * or ? · Issue #878 · mikefarah/yq
July 1, 2021 - Describe the bug If a user wants to find all strings which exactly match a pattern, however the pattern contains a * or ?, it will match unwanted entries. Version of yq: 4.9.3 Operating system: mac Installed via: homebrew Input Yaml data...
Author preethamrn
Thebottleneckdev
thebottleneckdev.com › blog › processing-yaml-files
YAML Processing with YQ: A Practical Guide | The Bottleneck Dev Blog
April 7, 2025 - Many configuration files (especially in Kubernetes) contain multiple YAML documents separated by ---. YQ can handle these easily: # Process all documents yq 'all(.metadata.name)' multi-doc.yaml # Process specific document yq 'select(documentIndex == 0).metadata.name' multi-doc.yaml # Update all documents yq -i 'all(select(has("kind")) | .metadata.namespace = "production")' multi-doc.yaml # Add a new document yq -i '. as $item ireduce ([]; .
GitHub
github.com › mikefarah › yq › issues › 475
How can I search elements in an array? · Issue #475 · mikefarah/yq
June 25, 2020 - Hi, If I understand the doc correctly, I should be able to search the following array for an app called app2 apps: - name: app1 slackChannel: my-team - name: app2 autoSync: true But the following doesn't work: yq read myfile.yaml "apps[*...
Author yfried
Claude Code Plugins
claude-plugins.dev › skills › @laurigates › dotfiles › yq-yaml-processing
yq YAML Processing - Claude Skills - Claude Code Plugins
December 11, 2025 - # String interpolation yq '"Hello, \(.name)"' file.yaml # Convert to string yq '.port | tostring' file.yaml # String contains yq '.[] | select(.email | contains("@gmail.com"))' file.yaml # String starts/ends with yq '.[] | select(.name | test("^A"))' file.yaml yq '.[] | select(.file | test("\\.yaml$"))' file.yaml # Split string yq '.path | split("/")' file.yaml # Join array to string yq '.tags | join(", ")' file.yaml # Lowercase/uppercase yq '.name | downcase' file.yaml yq '.name | upcase' file.yaml # String substitution yq '.image | sub("v1.0"; "v2.0")' file.yaml