yq — Process YAML and JSON on the Command Line
Practical guide to yq (mikefarah): query, transform and edit YAML, JSON and XML on the command line with a jq-like syntax.
yq is a lightweight command-line processor for structured data – think jq, but primarily for YAML (plus JSON, XML, CSV, TOML and properties). Pay attention to which yq you have installed: this guide targets the Go-based mikefarah/yq with its own jq-like expression syntax (yq eval, yq '.foo', -i for in-place editing, -o=json for output). The older kislyuk/yq is a Python wrapper around jq and uses a different syntax, so the examples below won't work there as written. With yq you read values, filter lists, edit files in place and convert between formats, slotting cleanly into pipelines with tools like kubectl or docker compose.
Read & Query
yq <file> — Pretty-print a YAML file.
yq config.yamlyq '.<key>' <file> — Read a specific key.
yq '.server.port' config.yamlyq '.<key>.<nested>' <file> — Read a nested value.
yq '.database.connection.host' config.yamlyq '.<array>[<n>]' <file> — Read an array element by index.
yq '.services[0].name' docker-compose.ymlyq '.<array>[]' <file> — Iterate over all array elements.
yq '.users[].email' config.yamlyq '.[] | key' <file> — List all top-level keys.
yq '.[] | key' config.yamlModify & Update
yq '.<key> = "<value>"' <file> — Set a value (outputs to stdout).
yq '.server.port = 8080' config.yamlyq -i '.<key> = "<value>"' <file> — Set a value in-place (modifies the file).
yq -i '.version = "2.0.0"' config.yamlyq '.<key> += "<value>"' <file> — Append a value to an array.
yq -i '.tags += ["new-tag"]' config.yamlyq 'del(.<key>)' <file> — Delete a key.
yq -i 'del(.deprecated_setting)' config.yamlyq '.<key> |= . + 1' <file> — Increment a numeric value.
yq -i '.build_number |= . + 1' version.yamlFilter & Select
yq '.[] | select(.<key> == "<value>")' <file> — Select elements matching a condition.
yq '.users[] | select(.role == "admin")' users.yamlyq '.[] | select(.<key> > <n>)' <file> — Select elements with numeric comparison.
yq '.items[] | select(.price > 100)' catalog.yamlyq '.<array> | length' <file> — Count elements in an array.
yq '.services | length' docker-compose.ymlyq '.[] | keys' <file> — List keys of a map.
yq '.services | keys' docker-compose.ymlyq 'has("<key>")' <file> — Check if a key exists.
yq 'has("database")' config.yamlFormat Conversion
yq -o=json <file> — Convert YAML to JSON.
yq -o=json config.yaml > config.jsonyq -p=json <file> — Read JSON input and output as YAML.
yq -p=json config.json > config.yamlyq -p=json -o=yaml <file> — Convert JSON to YAML (explicit).
yq -p=json -o=yaml package.jsonyq -o=xml <file> — Convert YAML to XML.
yq -o=xml config.yamlyq -o=csv <file> — Convert YAML to CSV.
yq -o=csv '.users' data.yamlyq -o=props <file> — Convert YAML to Java properties format.
yq -o=props config.yamlMerge & Multi-Document
yq '. * load("<file>")' <base> — Deep merge two YAML files (second overrides first).
yq '. * load("overrides.yaml")' base.yamlyq eval-all '. as $item ireduce ({}; . * $item)' <file1> <file2> — Merge multiple files together.
yq eval-all '. as $item ireduce ({}; . * $item)' defaults.yaml env.yamlyq -s '"doc_" + $index' <file> — Split a multi-document YAML into separate files.
yq -s '"doc_" + $index' multi.yamlyq 'select(documentIndex == 0)' <file> — Select a specific document from a multi-doc YAML.
yq 'select(documentIndex == 0)' multi.yamlPiping & Common Patterns
cat <file> | yq '.<key>' — Read from stdin.
kubectl get pod mypod -o yaml | yq '.metadata.labels'yq -r '.<key>' <file> — Raw output (no quotes around strings).
yq -r '.image' deployment.yamlyq -i '.image = "nginx:1.25"' deployment.yaml — Update a container image version in a Kubernetes manifest.
yq -i '.spec.template.spec.containers[0].image = "nginx:1.25"' deployment.yamlyq '.env_file' docker-compose.yml — Inspect Docker Compose config from the command line.
yq '.services.web.environment' docker-compose.yml Conclusion
yq makes reading and reshaping YAML, JSON and friends on the command line as convenient as jq is for JSON – perfect for pipelines with kubectl, docker compose or CI scripts. Keep in mind, though, that every example here targets mikefarah/yq (Go); with the Python wrapper kislyuk/yq many expressions look different. Take particular care with -i (in-place): yq overwrites the file directly and without prompting – make a backup first or work under version control, and test new expressions without -i by checking the output on stdout first. Note too that while yq largely preserves comments and formatting when editing in place, it may still normalize the file.
Further Reading
- mikefarah/yq documentation – the official manual with every operator and plenty of examples
- mikefarah/yq on GitHub – source code, installation instructions and release notes