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 Overflow
🌐
yq
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 ·
Discussions

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
🌐 github.com
2
1
July 30, 2022
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
🌐 github.com
1
2
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
🌐 github.com
14
October 29, 2019
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
🌐 github.com
5
February 10, 2022
🌐
yq
mikefarah.gitbook.io › yq › recipes
Recipes | yq - GitBook
Scalars, we just need the key and quoted value: ( select(kind == "scalar") | key + "='" + . + "'") Sequences (or arrays) are trickier, we need to quote each value and join them with ,: map("'" + . + "'") | join(",") Like the previous example, but lets handle nested data structures.
🌐
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
🌐
MetaCPAN
metacpan.org › dist › ETL-Yertl › view › bin › yq
yq - Filter YAML through a command-line program - metacpan.org
# INPUT foo: bar baz: fuzz --- foo: 1 baz: 2 $ yq '{ bar: .foo, .baz: foo }' bar: bar fuzz: foo --- 2: foo bar: 1
🌐
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 ·
Find elsewhere
🌐
Medium
baturorkun.medium.com › learning-to-manipulate-yaml-on-the-command-line-with-yq-531268d8805d
Learning to Manipulate YAML on the Command Line with YQ | by Batur Orkun | Medium
October 31, 2023 - yq e '.staff[] | select(.name == "Ebru") | .city' your_yaml_file_1.yaml · You can easily see which one is smooth and understandable. Maybe your YAML is a little more complex.
🌐
Baeldung
baeldung.com › home › files › file editing › processing yaml content with yq
Processing YAML Content With yq | Baeldung on Linux
March 18, 2024 - $ yq personal_data.yaml name: "My name" surname: "Foo" street_address: city: "My city" street: "My street" number: 1 · We should use the path in the YAML file to retrieve the property’s value.
🌐
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 ...
🌐
Better Programming
betterprogramming.pub › my-yq-cheatsheet-34f2b672ee58
My Cheatsheet for the “yq” Tool. Parsing Kubernetes YAML with yq | by Stefanie Lai | Better Programming
August 8, 2022 - My Cheatsheet for the “yq” Tool Parsing Kubernetes YAML with yq yq is a tool that mainly focuses on YAML data processing and highlighting. Its current 4.x version has made a huge upgrade based on …
🌐
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