Skills2026年4月14日·1 分钟阅读

yq — jq for YAML, JSON, XML, TOML, and .properties

yq is the `jq` for YAML. It reads/writes YAML, JSON, XML, TOML, and properties files with jq-compatible syntax — essential for DevOps, CI pipelines, and any config-heavy workflow.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install a6b08736-380a-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
yq extends jq's query language to YAML, JSON, XML, TOML, and properties files as a single dependency-free Go binary.
§01

What it is

yq (Mike Farah's Go version) brings jq's query and transformation language to YAML, JSON, XML, TOML, .properties, and CSV files. If you have ever needed to patch a Kubernetes manifest, extract a value from a Helm chart, or merge configuration files in a CI pipeline, yq handles it with a familiar jq-compatible syntax.

DevOps engineers, SREs, and platform teams use yq anywhere YAML is involved -- which in the Kubernetes ecosystem means nearly everywhere. It is a single Go binary with no runtime dependencies.

§02

How it saves time or tokens

yq replaces fragile sed/awk/grep pipelines for editing structured data. Instead of writing regex patterns that break when indentation changes, you use path expressions that understand YAML structure. The command yq '.spec.replicas = 5' deployment.yaml is safer and more readable than the equivalent sed command.

For AI workflows, yq can pre-process configuration files before feeding them to an LLM, extracting only the relevant sections to reduce prompt token count.

§03

How to use

  1. Install yq:
brew install yq          # macOS
sudo snap install yq     # Linux
  1. Query a YAML file:
yq '.spec.replicas' deployment.yaml
  1. Update a value in place:
yq -i '.spec.replicas = 5' deployment.yaml
§04

Example

Merging two YAML files and converting to JSON:

# Merge base config with environment overlay
yq ea '. as $item ireduce ({}; . * $item)' base.yaml prod.yaml

# Convert YAML to JSON
yq -o=json deployment.yaml

# Extract all container images from a K8s manifest
yq '.spec.template.spec.containers[].image' deployment.yaml

These one-liners replace multi-line scripts that would otherwise require Python or a custom parser.

§05

Related on TokRepo

  • DevOps Tools -- Infrastructure and deployment tools for Kubernetes workflows
  • Automation Tools -- CI/CD pipeline tools that work with config management
§06

Common pitfalls

  • There are two popular tools both named yq. The Go version by Mike Farah (most popular) and the Python version by Andrey Kislyuk use different syntax. Ensure you install the Go version (mikefarah/yq) for jq-compatible expressions.
  • Multi-document YAML files require the ea (evaluate all) command instead of plain e. Without it, yq only processes the first document.
  • Comments are preserved by default, which is usually desirable. However, format conversion (YAML to JSON) strips comments since JSON does not support them.

常见问题

What is the difference between yq and jq?+

jq only processes JSON. yq extends the same expression syntax to YAML, XML, TOML, CSV, and properties files. If you know jq, you already know most of yq's syntax. yq can also convert between formats (YAML to JSON, XML to YAML, etc.).

Can yq edit files in place?+

Yes. Use the `-i` flag: `yq -i '.key = value' file.yaml`. This modifies the file directly without needing to redirect output. The original formatting and comments are preserved.

Does yq handle Kubernetes multi-document YAML?+

Yes. Use `yq ea` (evaluate all) to process all documents in a multi-document YAML file separated by `---`. You can also select specific documents by index.

How do I install yq in a Docker container?+

Use the official Docker image: `docker run --rm -i mikefarah/yq < file.yaml`. Alternatively, download the static binary in your Dockerfile: `ADD https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 /usr/bin/yq`.

Can yq validate YAML syntax?+

yq will error on malformed YAML, so passing a file through yq serves as a basic syntax check. For schema validation against specific structures, combine yq with tools like kubeval or CUE.

引用来源 (3)
  • yq GitHub— yq is a jq-compatible processor for YAML, JSON, XML, TOML, and properties files
  • yq Documentation— yq usage and expression syntax documentation
  • jq GitHub— jq is a lightweight command-line JSON processor

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产