What is yq command

Last updated: April 2, 2026

Quick Answer: yq is a lightweight command-line YAML and JSON query processor that allows you to parse, filter, and manipulate YAML and JSON files directly in the terminal, similar to how jq works for JSON. First released in 2015, yq has become essential for DevOps engineers and developers managing configuration files, with over 2 million monthly downloads on GitHub. It supports multiple input/output formats including YAML, JSON, XML, and CSV, making it invaluable for shell scripts, CI/CD pipelines, and infrastructure automation tasks requiring data transformation and extraction.

Key Facts

Overview

yq is a powerful command-line tool designed to parse, filter, transform, and output YAML and JSON data with precision and efficiency. Originally created in 2015 as a Python implementation, yq has evolved into a sophisticated Go-based utility that processes structured data formats as easily as text-based tools like grep or sed process plain text. The tool fills a critical gap in Unix command-line utilities, providing YAML and JSON manipulation capabilities comparable to jq's JSON processing features. yq allows users to select specific fields, filter arrays, merge documents, and transform data structures directly from the command line, making it indispensable for DevOps professionals, system administrators, infrastructure engineers, and full-stack developers who work with configuration files, Kubernetes manifests, Docker Compose files, Ansible playbooks, and API responses.

Key Technical Details

yq operates using a sophisticated query language that supports path expressions similar to XPath but optimized for YAML structures. The tool can read from files, standard input, or multiple sources simultaneously and output results in various formats including YAML, JSON, XML, CSV, TSV, and plain text. A typical yq command follows the pattern: yq '[query expression]' [file.yaml], where the query expression defines what data to extract or transform. For example, the command yq '.spec.containers[0].image' deployment.yaml extracts the Docker image reference from a Kubernetes deployment file's first container specification. The tool supports complex operations including conditional filtering with the select() function, recursive descent using the .. operator, array flattening, object merging, and custom transformations. yq version 4.x introduced significant improvements including better path expression handling, streaming input for large files, and SQL-like query capabilities. The Go implementation processes YAML documents with streaming support, meaning it can handle files larger than available RAM by processing data in chunks rather than loading entire files into memory. yq's architecture leverages the gopkg.in/yaml.v3 library for YAML parsing, ensuring compliance with YAML 1.2 specifications while maintaining backward compatibility with YAML 1.1 documents.

Common Misconceptions

Many users mistakenly believe that yq is simply a YAML-only version of jq, when in reality it has evolved into a more comprehensive tool that handles YAML, JSON, XML, and other formats simultaneously. While jq predates yq and focuses exclusively on JSON, yq provides broader functionality including native YAML support with comment preservation in many operations and multiple format conversion capabilities. Another widespread misconception is that yq requires complete understanding of its advanced query syntax to be useful; in practice, 80% of common use cases involve simple dot notation queries or basic array indexing that developers intuitively understand. The third major misunderstanding concerns performance: many assume yq processes data slowly compared to native tools, but benchmarks demonstrate that the modern Go implementation actually processes structured data faster than equivalent sed/awk combinations or Python scripts, particularly for complex transformations. Additionally, users sometimes believe yq is limited to reading operations, when it actually supports powerful write and update operations that can modify YAML structures, add new fields, delete existing sections, and merge multiple documents together.

Practical Considerations and Use Cases

In real-world applications, yq proves invaluable for Kubernetes administrators managing hundreds of resource manifests. A typical workflow involves using yq to extract image references from all deployment files for security auditing: yq '.spec.template.spec.containers[].image' *.yaml | sort | uniq. DevOps engineers use yq in CI/CD pipelines to automatically update configuration values during deployment. For instance, a GitLab CI/CD pipeline might use yq -i '.image.tag = env(VERSION)' config.yaml to dynamically update version numbers before deployment. Infrastructure teams leverage yq for bulk operations: extracting all environment variables from Compose files, migrating configurations between different systems, or validating that manifests comply with organizational standards. Cloud engineers employ yq when working with AWS CloudFormation, Terraform configurations, and Helm charts to parse and manipulate YAML-based infrastructure definitions. The tool becomes particularly powerful when combined with shell scripting and automation frameworks, where it enables sophisticated data pipelines that extract metrics from structured logs, transform API responses, and generate reports from configuration data. For developers building command-line tools, yq provides a reference implementation for YAML processing and serves as a model for understanding YAML semantics and edge cases. Security teams use yq to audit configuration files systematically, extracting security-relevant fields like image registries, resource limits, and access controls from infrastructure code.

Related Questions

How is yq different from jq?

While jq exclusively processes JSON data, yq handles YAML, JSON, XML, and CSV formats natively. jq was released in 2013 and inspired yq's creation in 2015. yq's main advantage is native YAML support with comment preservation and simultaneous multi-format processing, whereas jq excels in JSON-specific operations and has a larger ecosystem of complementary tools and resources.

How do you install yq on different operating systems?

yq can be installed via package managers: on macOS using Homebrew with 'brew install yq', on Ubuntu/Debian using 'sudo apt-get install yq', on RHEL/CentOS using 'sudo yum install yq', and on Windows using Chocolatey with 'choco install yq'. Pre-compiled binaries are also available on GitHub for every major operating system, allowing direct download and execution without package manager dependencies.

Can yq modify YAML files in place?

Yes, yq supports in-place editing using the -i or --inplace flag. The command 'yq -i '.spec.replicas = 3' deployment.yaml' modifies the deployment.yaml file directly without creating a backup, unless the -i flag is followed by a suffix like -i.bak to create backup files. This feature is essential for automation scripts that need to update configuration files while preserving formatting and comments.

What are the most commonly used yq operators?

Essential operators include the pipe operator (|) for chaining operations, select() for filtering arrays based on conditions, keys/keys_unsorted for extracting object keys, length for array/string lengths, group_by for organizing data, and unique for deduplication. The recursive descent operator (..) finds all instances of a key at any depth, while the alternative operator (//) provides default values when keys are missing or null.

How do you handle multiple YAML files with yq?

yq can process multiple files using glob patterns like 'yq '.metadata.name' *.yaml' to read all YAML files in a directory. The -M flag processes each file separately, --slurpfile loads one file into a variable for reference within queries, and the -s flag merges multiple documents into an array. For advanced operations, users can combine yq with shell loops: 'for file in *.yaml; do yq -i '.version = "1.2.3"' "$file"; done'.

Sources

  1. yq - Official GitHub RepositoryMIT
  2. yq - Official DocumentationMIT
  3. jq - Official DocumentationCC0 1.0
  4. YAML Specification Version 1.2CC0 1.0