YAML with yq: Query, Edit, and Transform

High-value yq commands for reading, updating, filtering, converting, and merging YAML in local scripts and CI pipelines.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Read and query YAML with yq

Extract values and slice nested structures from YAML files.

Read a nested field

Print a nested value from YAML.

bashANYyamlyqquery
bash
yq '.spec.template.spec.containers[0].image' deployment.yaml

Excellent for scripts, CI jobs, and quick inspections.

Read a list

Print array items from a YAML path.

bashANYyamlyqarray
bash
yq '.features[]' config.yaml

Useful for iterating over YAML sequences in shell scripts.

Filter objects in a sequence

Select matching records from a YAML array.

bashANYyamlyqfilter
bash
yq '.users[] | select(.role == "admin")' users.yaml

A practical pattern when using YAML as config or fixture data.

Convert YAML to JSON

Transform YAML into JSON for downstream tools.

bashANYyamljsonconversion
bash
yq -o=json '.' config.yaml

Helpful when piping YAML into tools that only understand JSON.

Edit and transform YAML with yq

Update files safely from scripts or release automation.

Set an image tag

Update a nested container image value in place.

bashANYyamlyqeditkubernetes
bash
yq -i '.spec.template.spec.containers[0].image = "repo/api:v2"' deployment.yaml

Useful for release pipelines that stamp manifests with a new image tag.

Append to a YAML array

Add a value to a sequence in place.

bashANYyamlyqappend
bash
yq -i '.features += ["coach me"]' config.yaml

A clean way to automate config changes without manual editing.

Delete a key

Remove an unwanted field from YAML.

bashANYyamlyqdelete
bash
yq -i 'del(.metadata.annotations)' deployment.yaml

Helpful when stripping environment-specific or generated data.

Merge YAML files

Combine multiple YAML files into one document.

bashANYyamlyqmerge
bash
yq ea '. as $item ireduce ({}; . * $item )' base.yaml override.yaml

Common in layered configuration workflows.

Recommended next

No recommendations yet.