$linuxjunkies
>

yq(1)

yq is a lightweight YAML, JSON, XML and CSV query and transformation tool that works like jq but for structured data formats.

UbuntuDebianFedoraArch

Synopsis

yq [FLAGS] [EXPRESSION] [FILE]...

Description

yq is a command-line YAML processor that lets you query, filter, update, and transform YAML documents. It uses an expression syntax similar to jq, making it intuitive for those familiar with JSON querying. It also handles JSON, XML, and CSV formats transparently.

You can use yq to extract values, modify structure, merge documents, and convert between formats. It's commonly used in scripts, CI/CD pipelines, and configuration management workflows.

Common options

FlagWhat it does
-r, --raw-outputOutput raw strings without quotes; useful for shell integration
-y, --yaml-outputOutput results in YAML format (default for most operations)
-j, --json-outputOutput results in JSON format
-x, --xml-outputOutput results in XML format
-c, --compact-outputCompact JSON output (no pretty-printing)
-i, --inplaceModify files in-place instead of printing to stdout
-s, --slurpRead entire input stream into array before processing
-n, --null-inputDon't read input; useful for generating new documents
-M, --no-colorsDisable colored output
-e, --exit-statusSet exit code based on output (0 if truthy, 1 if false/null)

Examples

Extract the 'name' field from a YAML file

yq '.name' config.yaml

Filter array items where status equals 'active'

yq '.items[] | select(.status == "active")' data.yaml

Update a nested value in-place

yq '.database.host = "localhost"' config.yaml -i

Extract all email addresses as raw strings from an array

yq -r '.users[].email' users.yaml

Extract config object and output as JSON

yq -j '.config' data.yaml

Merge multiple YAML files by slurping them into an array and adding

yq -s 'add' *.yaml

Generate a new YAML document from scratch

yq -n '{name: "app", version: "1.0"}' -y

Read JSON from stdin, filter, and output as YAML

cat data.json | yq '.[] | select(.id > 5)' -y

Related commands