The trick is to convert the object into an array, apply 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 both implementations.
#!/bin/bash
INPUT='
{
"entries": {
"entry1": {
"enabled": true
},
"entry2": {
"enabled": false
},
"entry3": {
"dummy": "TEST"
}
}
}
'
echo "yq - Go implementation" # https://mikefarah.gitbook.io/yq/
yq -j e '.entries | with_entries(select(.value.enabled == true))' - <<< "$INPUT"
echo "yq - Python implementation" # https://github.com/kislyuk/yq
yq '.entries | with_entries(select(.value.enabled == true))' <<< "$INPUT"
output
{
"entry1": {
"enabled": true
}
}
Answer from jpseng on Stack Overflowyq
mikefarah.gitbook.io › yq › operators › select
Select | yq - GitBook
yq '.[] | select(. == "*at")' sample.yml · will output · Copy · cat goat · Given a sample.yml file of: then · will output · Given a sample.yml file of: then · will output · See more regular expression examples under the string operator docs. Given a sample.yml file of: then ·
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
Using `or` in a `select`
I have a multi-document YAML file and I'm trying to filter it down to a smaller set of documents based on a boolean expression that uses or. Here's a reproducer: Version yq version: 4.7.1 I... More on github.com
How do I select multiple keys for output?
This seems like a basic question but I can't find examples anywhere in the documentation. If I have input like - alpha: 1 beta: foo gamma: 0.3 delta: bar - alpha: 2 beta: baz gamma: 1.23 delta: buz... More on github.com
Selecting elements from map could be simplified
From what I have seen so far, selecting elements from map using with_entries/select idiom could be simplified by using styles similar to how jq does country: name: Australia capital: Canberra type: Continent And we run a command: yq '.co... More on github.com
Thebottleneckdev
thebottleneckdev.com › blog › processing-yaml-files
YAML Processing with YQ: A Practical Guide | The Bottleneck Dev Blog
April 7, 2025 - # Add to an array yq -i '.spec.containers += {"name":"sidecar", "image":"nginx"}' deployment.yaml # Filter array elements yq '.spec.containers[] | select(.name == "app")' deployment.yaml # Map over array elements yq '.items[] | .metadata.name' resources.yaml # Sort an array yq -i '.spec.containers |= sort_by(.name)' deployment.yaml
HackingNote
hackingnote.com › en › cheatsheets › yq
Cheatsheet - yq
$ yq '.[] | select(.name == "Foo")' example.yml · Increment numBuckets: $ yq '(.[] | select(.name == "Foo") | .numBuckets) |= . + 1' sample.yml · |= is the operator that updates fields relative to their own value, which is referenced as dot (.) Sort an array by a field ·
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
Kislyuk
kislyuk.github.io › yq
yq: Command-line YAML/XML/TOML processor
For example, a jq filter that counts entries in the Instances array will come up with 4 entries instead of 2. A filter that expects all array entries to be mappings may break due to the presence of string metadata keys. Check your jq filter for compatibility/semantic validity when using the ...
GitHub
github.com › mikefarah › yq
GitHub - mikefarah/yq: yq is a portable command-line YAML, JSON, XML, CSV, TOML, HCL and properties processor · GitHub
# Note: requires input file - add your file at the end yq -i '(.[] | select(.name == "foo") | .address) = "12 cat st"' data.yaml · Convert between formats: # Convert JSON to YAML (pretty print) yq -Poy sample.json # Convert YAML to JSON yq -o json file.yaml # Convert XML to YAML yq -o yaml file.xml · See recipes for more examples and the documentation for more information.
Starred by 15.1K users
Forked by 754 users
Languages Go 90.7% | Shell 8.9%
GitHub
github.com › mikefarah › yq › issues › 287
How do I select multiple keys for output? · Issue #287 · mikefarah/yq
October 29, 2019 - This seems like a basic question but I can't find examples anywhere in the documentation. If I have input like - alpha: 1 beta: foo gamma: 0.3 delta: bar - alpha: 2 beta: baz gamma: 1.23 delta: buzz And I want to select only the beta and...
Author ronniegane
Linkml
linkml.io › linkml › howtos › yq-for-schemas.html
Using yq for querying and manipulating schemas - linkml documentation
The examples here make use of the PersonInfo schema ... ✗ yq e '.classes | keys' personinfo.yaml - NamedThing - Person - HasAliases - Organization - Place - Address - Event - Concept - DiagnosisConcept - ProcedureConcept - Relationship - FamilialRelationship - EmploymentEvent - MedicalEvent - WithLocation # TODO: annotate that this is a container/root class - Container
GitHub
github.com › mikefarah › yq › issues › 1102
Selecting elements from map could be simplified · Issue #1102 · mikefarah/yq
February 10, 2022 - From what I have seen so far, selecting elements from map using with_entries/select idiom could be simplified by using styles similar to how jq does · country: name: Australia capital: Canberra type: Continent · And we run a command: yq '.country |= with_entries(select(.key == "name" or .key == "capital"))' yaml ·
Author inianv
Martin Heinz
martinheinz.dev › blog › 51
Mastering YAML Processing in Command Line | Martin Heinz | Personal Website & Blog
June 15, 2021 - And finally, you might also find ... first example we looked at): # yq eval ".user.addresses[]" user.yaml street: "Oxford Street" city: London country: England street: "Ludwigstrasse" city: Munich country: Germany · Apart from basic traversing you might also want to get familiar with selecting, which allows ...
Stack Overflow
stackoverflow.com › questions › 79403259 › how-to-use-yq-on-multiple-documents-selected-by-content-not-by-index
How to use yq on multiple documents, selected by content not by index - Stack Overflow
(Obviously this is a super simple example and a real manifest can contain hundreds of documents and even some for the same kind. so I'd need something like kind=="Deployment" & name="nginx") ... You can select by any filter you want, e.g. by matching some parts against some literals, and then just modify the selected items while still keeping the top-level context to output all documents: yq 'select(.kind == "Deployment").spec.image = "alpine"' example.yaml
GitHub
github.com › mikefarah › yq › blob › master › README.md
yq/README.md at master · mikefarah/yq
# Note: requires input file - add your file at the end yq -i '(.[] | select(.name == "foo") | .address) = "12 cat st"' data.yaml ... # Convert JSON to YAML (pretty print) yq -Poy sample.json # Convert YAML to JSON yq -o json file.yaml # Convert XML to YAML yq -o yaml file.xml · See recipes for more examples and the documentation for more information.
Author mikefarah