cheat-sheets

yq

yq (mikefarah/yq, version 4) is a command-line processor for YAML, with JSON, XML, CSV, TOML, properties and INI support. Its expressions are close to jq. Use it to read and edit YAML such as Docker Compose files, Kubernetes manifests and CI pipelines. There is a different, Python-based tool also called yq; this sheet covers the Go one.

Project: mikefarah/yq Documentation: yq docs

Verification: flags and every example were run with yq v4.47.2 (Alpine package) in a throwaway container. Behaviour of yq from a package manager may be the Python yq instead: check yq --version.

See also: jq · Docker-Compose · Helm Cheat-Sheet · Ansible Cheat-Sheet · Modern CLI Tools


Reading #

Examples use this file:

yaml
services:
  web:
    image: nginx:1.27
    ports: ["80:80", "443:443"]
  db:
    image: postgres:17
COMMANDDESCRIPTION
yq '.services.web.image' FILE.ymlOne value
yq '.services | keys' FILE.ymlKeys of a map
yq '.services.web.ports[0]' FILE.ymlArray element
yq '.services.web.ports[]' FILE.ymlEvery element, one per line
yq '.services.*.image' FILE.ymlWildcard over map values
yq '.services.web.ports | length' FILE.ymlCount
yq '.services[] | select(.image == "postgres*")' FILE.ymlFilter (== accepts * globs)
yq '.services[] | select(.image | test("^postgres"))' FILE.ymlFilter by regular expression
yq '.services | to_entries | .[].key' FILE.ymlKeys via entries
yq '.a.b' < FILE.ymlRead from stdin
yq -e '.missing' FILE.ymlExit non-zero when nothing matches (prints null)

Editing #

COMMANDDESCRIPTION
yq -i '.services.web.image = "nginx:1.28"' FILE.ymlChange a value in place
yq '.services.web.image = "nginx:1.28"' FILE.ymlPrint the result without saving
yq '.services.web.ports += ["8080:80"]' FILE.ymlAppend to an array
yq 'del(.services.db)' FILE.ymlDelete a key
yq '(.services[] | select(.image == "postgres:17") | .image) = "postgres:16"' FILE.ymlUpdate matching nodes
yq '.a.b |= . + 1' FILE.ymlUpdate using the current value
yq '.services.web.environment.TZ = strenv(TZ_VALUE)' FILE.ymlSet from an environment variable as a string
yq '.a.b = env(NUMBER)' FILE.ymlSet from an environment variable, parsed as YAML (numbers stay numbers)
yq '.services.web.image | sub("nginx:", "")' FILE.ymlString replacement (regex) on a value
yq 'sort_keys(.)' FILE.ymlSort map keys
yq -n '.a.b = 1'Build a document from nothing (-n)

-i rewrites the file. Commit or copy it first.


Merging and Multiple Files #

COMMANDDESCRIPTION
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' A.yml B.ymlDeep-merge two files (the second wins)
yq eval-all '. as $item ireduce ({}; . * $item)' A.yml B.ymlMerge any number of files
yq '.c = load("OTHER.yml").a' FILE.ymlPull a value in from another file
yq 'explode(.)' FILE.ymlExpand anchors and aliases
yq '.a' MULTI_DOC.ymlRuns against each document in a multi-document file

eval is the default command: it runs per document. eval-all loads every document of every file first.


Formats #

COMMANDDESCRIPTION
yq -o=json '.' FILE.ymlYAML to JSON
yq -o=json -I=0 '.' FILE.ymlCompact JSON on one line
yq -p=json -o=yaml '.' FILE.jsonJSON to YAML (also -P to pretty print)
yq -o=props '.' FILE.ymlJava properties style
yq -p xml '.' FILE.xmlRead XML
yq -s '.services.web.image' FILE.ymlSplit results into files named by the expression
yq -N '.' FILE.ymlOmit the --- document separators

Formats accepted by -p / -o: yaml, json, props, csv, tsv, xml, base64, uri, toml, lua, ini (and shell for output).


Troubleshooting #

SYMPTOMFIX
yq: command not found or wrong syntaxCheck which yq you have: yq --version should name mikefarah/yq
Formatting changed after -iQuote style and list style can change (a += on a flow list was printed with single quotes in testing): review the diff
Expression taken as a file nameAdd --expression '<EXPR>'
Shell expanded $VAR or *Quote the expression with single quotes

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.