Skip to content
Latchkey

yq with_entries and to_entries: Transform Maps

yq with_entries maps over a map as {key, value} pairs, letting you rename keys or filter entries in one pass.

When you need to touch every key in a map, such as prefixing labels or dropping empty values, with_entries is the clean tool, mirroring the jq function of the same name.

What it does

to_entries turns a map {a: 1} into a list [{key: "a", value: 1}]. from_entries does the reverse. with_entries(f) is shorthand for to_entries | map(f) | from_entries, letting you transform each entry by its .key and .value.

Common usage

Terminal
# prefix every label key
yq '.metadata.labels |= with_entries(.key |= "app." + .)' deploy.yaml
# drop entries whose value is null
yq '.data |= with_entries(select(.value != null))' configmap.yaml
# uppercase all values
yq '.data |= with_entries(.value |= upcase)' configmap.yaml

Entry functions

FunctionWhat it does
to_entriesMap to a list of {key, value} pairs
from_entriesList of pairs back to a map
with_entries(f)to_entries | map(f) | from_entries
.key / .valueThe fields of each entry
select(.value != null)Filter entries by their value

In CI

To bulk-rename or namespace ConfigMap keys before apply: yq -i '.data |= with_entries(.key |= "PROD_" + .)' configmap.yaml. The |= update-assign writes the transformed map back to the same path.

Common errors in CI

with_entries on a value that is a list, not a map, errors because to_entries expects a map (or array); check the node type first. Forgetting |= and writing .data = with_entries(...) without . in scope loses the original; use the update-assign form. These functions exist in both Go yq and jq, so jq examples often transfer, but Python yq still needs -i emulation to persist edits.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →