yq 4.53.3
Overview
yq is a lightweight, portable command-line processor for YAML, JSON, XML, INI, TOML, HCL, CSV, TSV, Properties, Lua, Shell variables, and Base64. It uses jq-like expression syntax but works across all these formats. Written in Go — single binary, no dependencies.
Key capabilities:
- Read, update, delete, and create structured data via jq-style expressions
- In-place file editing (
-i)
- Format conversion between any supported pair (YAML↔JSON, XML→YAML, TOML↔HCL, etc.)
- Multi-document YAML support
- Environment variable injection (
env(), strenv(), envsubst())
- File loading (
load(), load_xml(), load_props())
- Comment and anchor/alias preservation during edits
- Reduce/merge across multiple files with
eval-all
Usage
Basic syntax
yq [flags] 'expression' [file ...]
yq [flags] 'expression' < file # pipe from stdin
yq -n 'expression' # no input, create from scratch
Common operations
Read a value:
yq '.a.b[0].c' file.yaml
Update in place:
yq -i '.a.b[0].c = "cool"' file.yaml
Use environment variables:
NAME=mike yq -i '.a.b[0].c = strenv(NAME)' file.yaml
Multiple updates in one pass:
yq -i '.a = "x" | .b.c = "y"' file.yaml
Find and update in an array:
yq -i '(.[] | select(.name == "foo") | .address) = "12 cat st"' data.yaml
Convert formats:
yq -Poy sample.json # JSON → pretty YAML
yq -o json file.yaml # YAML → JSON
yq -o yaml file.xml # XML → YAML
Merge multiple files:
yq -n 'load("a.yaml") * load("b.yaml")'
yq ea '. as $item ireduce ({}; . * $item)' path/*.yml
Create from scratch:
yq -n '.a.b = "cat" | .x = "frog"'
Key flags
| Flag |
Description |
-i |
Update file in place |
-n |
Null input (create from scratch) |
-P |
Pretty print (... style = "") |
-r |
Unwrap scalar (no quotes) |
-o fmt |
Output format: yaml, json, xml, toml, hcl, csv, tsv, props, ini, lua, shell, base64, kyaml |
-p fmt |
Input format (default auto from extension) |
-I n |
Output indent level (default 2) |
-N |
No document separators (---) |
-e |
Exit status based on result |
-s exp |
Split output into files named by expression |
--indent |
Same as -I |
--yaml-fix-merge-anchor-to-spec |
Fix merge anchor behavior to YAML spec |
Expression commands
yq eval 'expr' file.yaml # default: process each document in sequence
yq eval-all 'expr' file.yaml # load all docs from all files, run once (alias: yq ea)
Gotchas
- PowerShell quoting — use single quotes for expressions, or escape double quotes. PowerShell expands
$() and "" inside strings.
- Bash trailing newlines —
$(cmd) strips trailing newlines. Use printf -v var "text\n" or multiline assignment to preserve them in YAML blocks.
- Merge anchor legacy behavior — by default, yq uses non-spec merge anchor semantics (later anchors override earlier ones). Add
--yaml-fix-merge-anchor-to-spec for correct YAML 1.2 behavior (earlier keys win).
yes/no are not booleans — YAML 1.2 dropped them as boolean values. They parse as strings.
- Comment preservation is imperfect — yq tries to preserve comments and whitespace during edits, but complex restructures may lose them.
- In-place editing (
-i) writes to the first file argument only. Subsequent files are read-only inputs.
env() parses YAML — env(VAR) interprets the value as YAML (so "true" becomes boolean). Use strenv(VAR) for raw strings.
- Numeric keys —
.0 traverses array index 0; use .["0"] to access a map key that is literally the string "0".
- Security flags — use
--security-disable-env-ops and --security-disable-file-ops when processing untrusted expressions.
References
- 01-traverse-read — Path navigation, splat, wildcards, dynamic keys
- 02-assign-update —
=, |=, create nodes, update in place
- 03-select-filter —
select(), filter, boolean and comparison operators
- 04-reduce-merge —
ireduce, merge files, collect into arrays/objects
- 05-format-conversion — Convert between YAML, JSON, XML, TOML, HCL, CSV, INI, Properties, Lua, Shell
- 06-string-operators — Regex (
test, match, capture, sub), interpolation, slicing
- 07-env-file-operators —
env(), strenv(), envsubst(), load(), file operations
- 08-datetime — Date/time parsing, formatting, timezone handling
- 09-advanced-features — Comments, style, tags, anchors/aliases, entries, path
1---2name: yq-4-53-33description: Query, transform, and convert YAML, JSON, XML, INI, TOML, HCL, CSV, TSV, Properties, Lua, Shell variables, and Base64 data using jq-like expressions. Use when the user mentions yq, YAML processing, JSON-to-YAML conversion, config file manipulation, Kubernetes YAML editing, or any structured data transformation task. Supports multi-document YAML, anchors/aliases, comments, and in-place updates.4---56# yq 4.53.378## Overview910`yq` is a lightweight, portable command-line processor for YAML, JSON, XML, INI, TOML, HCL, CSV, TSV, Properties, Lua, Shell variables, and Base64. It uses `jq`-like expression syntax but works across all these formats. Written in Go — single binary, no dependencies.1112Key capabilities:13- Read, update, delete, and create structured data via jq-style expressions14- In-place file editing (`-i`)15- Format conversion between any supported pair (YAML↔JSON, XML→YAML, TOML↔HCL, etc.)16- Multi-document YAML support17- Environment variable injection (`env()`, `strenv()`, `envsubst()`)18- File loading (`load()`, `load_xml()`, `load_props()`)19- Comment and anchor/alias preservation during edits20- Reduce/merge across multiple files with `eval-all`2122## Usage2324### Basic syntax2526```bash27yq [flags] 'expression' [file ...]28yq [flags] 'expression' < file # pipe from stdin29yq -n 'expression' # no input, create from scratch30```3132### Common operations3334**Read a value:**35```bash36yq '.a.b[0].c' file.yaml37```3839**Update in place:**40```bash41yq -i '.a.b[0].c = "cool"' file.yaml42```4344**Use environment variables:**45```bash46NAME=mike yq -i '.a.b[0].c = strenv(NAME)' file.yaml47```4849**Multiple updates in one pass:**50```bash51yq -i '.a = "x" | .b.c = "y"' file.yaml52```5354**Find and update in an array:**55```bash56yq -i '(.[] | select(.name == "foo") | .address) = "12 cat st"' data.yaml57```5859**Convert formats:**60```bash61yq -Poy sample.json # JSON → pretty YAML62yq -o json file.yaml # YAML → JSON63yq -o yaml file.xml # XML → YAML64```6566**Merge multiple files:**67```bash68yq -n 'load("a.yaml") * load("b.yaml")'69yq ea '. as $item ireduce ({}; . * $item)' path/*.yml70```7172**Create from scratch:**73```bash74yq -n '.a.b = "cat" | .x = "frog"'75```7677### Key flags7879| Flag | Description |80|---|---|81| `-i` | Update file in place |82| `-n` | Null input (create from scratch) |83| `-P` | Pretty print (`... style = ""`) |84| `-r` | Unwrap scalar (no quotes) |85| `-o fmt` | Output format: `yaml`, `json`, `xml`, `toml`, `hcl`, `csv`, `tsv`, `props`, `ini`, `lua`, `shell`, `base64`, `kyaml` |86| `-p fmt` | Input format (default `auto` from extension) |87| `-I n` | Output indent level (default 2) |88| `-N` | No document separators (`---`) |89| `-e` | Exit status based on result |90| `-s exp` | Split output into files named by expression |91| `--indent` | Same as `-I` |92| `--yaml-fix-merge-anchor-to-spec` | Fix merge anchor behavior to YAML spec |9394### Expression commands9596```bash97yq eval 'expr' file.yaml # default: process each document in sequence98yq eval-all 'expr' file.yaml # load all docs from all files, run once (alias: yq ea)99```100101## Gotchas102103- **PowerShell quoting** — use single quotes for expressions, or escape double quotes. PowerShell expands `$()` and `""` inside strings.104- **Bash trailing newlines** — `$(cmd)` strips trailing newlines. Use `printf -v var "text\n"` or multiline assignment to preserve them in YAML blocks.105- **Merge anchor legacy behavior** — by default, yq uses non-spec merge anchor semantics (later anchors override earlier ones). Add `--yaml-fix-merge-anchor-to-spec` for correct YAML 1.2 behavior (earlier keys win).106- **`yes`/`no` are not booleans** — YAML 1.2 dropped them as boolean values. They parse as strings.107- **Comment preservation is imperfect** — yq tries to preserve comments and whitespace during edits, but complex restructures may lose them.108- **In-place editing (`-i`)** writes to the first file argument only. Subsequent files are read-only inputs.109- **`env()` parses YAML** — `env(VAR)` interprets the value as YAML (so `"true"` becomes boolean). Use `strenv(VAR)` for raw strings.110- **Numeric keys** — `.0` traverses array index 0; use `.["0"]` to access a map key that is literally the string `"0"`.111- **Security flags** — use `--security-disable-env-ops` and `--security-disable-file-ops` when processing untrusted expressions.112113## References114115- [01-traverse-read](references/01-traverse-read.md) — Path navigation, splat, wildcards, dynamic keys116- [02-assign-update](references/02-assign-update.md) — `=`, `|=`, create nodes, update in place117- [03-select-filter](references/03-select-filter.md) — `select()`, `filter`, boolean and comparison operators118- [04-reduce-merge](references/04-reduce-merge.md) — `ireduce`, merge files, collect into arrays/objects119- [05-format-conversion](references/05-format-conversion.md) — Convert between YAML, JSON, XML, TOML, HCL, CSV, INI, Properties, Lua, Shell120- [06-string-operators](references/06-string-operators.md) — Regex (`test`, `match`, `capture`, `sub`), interpolation, slicing121- [07-env-file-operators](references/07-env-file-operators.md) — `env()`, `strenv()`, `envsubst()`, `load()`, file operations122- [08-datetime](references/08-datetime.md) — Date/time parsing, formatting, timezone handling123- [09-advanced-features](references/09-advanced-features.md) — Comments, style, tags, anchors/aliases, entries, path