yq
YAML/JSON/XML/CSV/TOML processor with jq-like syntax.
Official WebsiteFeatures
YAML Processingjq-like SyntaxMultiple FormatsIn-place Editing
Installation
Homebrew
brew install yqChocolatey
choco install yqWhy use yq?
yq is a tool for processing YAML files from the command line. With jq-like syntax for manipulating YAML, it's ideal for editing Kubernetes and CI/CD configurations.
Multi-Format Support
Convert between multiple formats including YAML, JSON, XML, TOML, and CSV.
jq-Compatible Syntax
Nearly zero learning curve for jq users. Use the same query syntax to manipulate YAML.
In-Place Editing
Edit files directly. Update while preserving comments and formatting.
Single Binary
Install without dependencies. Easy to use in CI/CD environments.
Installation
Installation
# macOS (Homebrew)
brew install yq
# Ubuntu/Debian (snap)
sudo snap install yq
# Download binary directly
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
chmod +x /usr/local/bin/yq
# Install via Go
go install github.com/mikefarah/yq/v4@latest
# Docker
docker run --rm -v "${PWD}":/workdir mikefarah/yqNote: There are multiple implementations of yq. This guide uses mikefarah/yq (Go-based). It's different from the Python-based kislyuk/yq.
Basic Usage
Reading YAML
Reading
# Display entire YAML file
yq '.' config.yaml
# Get specific field
yq '.metadata.name' deployment.yaml
# Nested fields
yq '.spec.containers[0].image' pod.yaml
# Get array elements
yq '.items[].name' list.yamlSample YAML
config.yaml
# config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
namespace: default
data:
database_url: postgres://localhost:5432/mydb
log_level: infoExtracting Values
Value Extraction
# Get as string (without quotes)
yq -r '.metadata.name' config.yaml
# Output: my-config
# Get multiple fields
yq '.metadata | {name, namespace}' config.yaml
# Get all keys
yq '.data | keys' config.yamlCommon Patterns
Manipulating Kubernetes Configurations
Kubernetes
# Get Deployment image
yq '.spec.template.spec.containers[0].image' deployment.yaml
# Get name and image of all containers
yq '.spec.template.spec.containers[] | {name, image}' deployment.yaml
# Check resource limits
yq '.spec.template.spec.containers[].resources' deployment.yaml
# Get labels
yq '.metadata.labels' deployment.yamlEditing YAML
Editing
# Update field value
yq '.metadata.name = "new-name"' config.yaml
# Add new field
yq '.metadata.labels.app = "myapp"' config.yaml
# Update nested value
yq '.spec.replicas = 3' deployment.yaml
# Add element to array
yq '.spec.containers += [{"name": "sidecar", "image": "nginx"}]' pod.yaml
# Delete field
yq 'del(.metadata.annotations)' deployment.yamlIn-Place Editing
In-Place Editing
# Update file directly (-i option)
yq -i '.metadata.name = "updated-name"' config.yaml
# Create backup while editing
yq -i '.version = "2.0"' config.yaml && cp config.yaml config.yaml.bak
# Batch update multiple files
for f in *.yaml; do
yq -i '.metadata.namespace = "production"' "$f"
doneAdvanced Usage
Format Conversion
Format Conversion
# Convert YAML to JSON
yq -o=json '.' config.yaml
# Convert JSON to YAML
yq -P '.' config.json
# Convert YAML to XML
yq -o=xml '.' config.yaml
# Multiple YAML documents to array
yq ea '[.]' file1.yaml file2.yamlMulti-Document Processing
Multi-Document
# Process all documents (multiple YAML separated by ---)
yq ea '.' multi-doc.yaml
# Select specific document
yq 'select(documentIndex == 1)' multi-doc.yaml
# Filter documents by condition
yq 'select(.kind == "Deployment")' resources.yaml
# Apply operation to all documents
yq ea '.metadata.namespace = "staging"' *.yamlMerging and Combining
Merging
# Merge two YAML files
yq ea '. as $item ireduce ({}; . * $item)' base.yaml override.yaml
# Combine arrays
yq ea '.items = ([.items] | flatten)' file1.yaml file2.yaml
# Embed environment variables
export IMAGE_TAG="v1.2.3"
yq '.spec.containers[0].image = env(IMAGE_TAG)' deployment.yaml
# Reference values from another file
yq '.data.config = load("settings.json")' configmap.yamlConditional Operations
Conditional Operations
# Update elements matching condition
yq '(.spec.containers[] | select(.name == "app")).image = "newimage:v2"' deployment.yaml
# Exclude null values
yq '.. | select(. != null)' config.yaml
# Extract only specific types
yq '.. | select(type == "!!str")' config.yaml
# Display with path
yq '.. | select(. == "production") | path' config.yamlCommon Options
| Option | Description | Example |
|---|---|---|
-i | In-place editing | yq -i '.v = 1' f.yaml |
-o=json | Output in JSON format | yq -o=json '.' f.yaml |
-P | Pretty print (YAML format) | yq -P '.' f.json |
-r | Raw string output | yq -r '.name' f.yaml |
ea | Evaluate all documents | yq ea '.' *.yaml |
-C | Force color output | yq -C '.' f.yaml |
Practical Examples
Updating CI/CD Configuration
CI/CD
# Update GitHub Actions version
yq -i '(.jobs.build.steps[] | select(.uses | test("actions/checkout"))).uses = "actions/checkout@v4"' .github/workflows/ci.yaml
# Add environment variable
yq -i '.env.NEW_VAR = "value"' .github/workflows/ci.yaml
# Update Docker Compose image tag
yq -i '.services.app.image = "myapp:$TAG"' docker-compose.yamlWorking with Helm values
Helm
# Update image tag in values.yaml
yq -i '.image.tag = "v2.0.0"' values.yaml
# Set replica count for environment
yq -i '.replicaCount = 3' values-production.yaml
# Merge multiple values files
yq ea '. as $item ireduce ({}; . * $item)' values.yaml values-override.yamlTips
- •Use
yq -Pto convert JSON to YAML. Useful for saving API responses in YAML format - •Use
env()function to embed environment variables in YAML. Great for dynamic values in CI/CD - •Comments are preserved by default. Use
-Noption to remove comments - •Combine
select()withtest()to filter elements with regex - •For complex queries, use
--from-fileto load from a file for easier management