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

jq — Lightweight Command-Line JSON Processor

jq is the essential command-line tool for processing JSON data. It lets you slice, filter, transform, and format JSON with a concise expression language — making it indispensable for working with APIs, config files, and data pipelines in the terminal.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
先审查命令
npx -y tokrepo@latest install f8b7c935-3712-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
jq lets you slice, filter, transform, and format JSON data from the command line with a concise expression language.
§01

What it is

jq is a lightweight command-line tool for processing JSON data. Think of it as sed for JSON -- it takes JSON input, applies filters and transformations using a concise expression language, and outputs the result. jq handles extraction, filtering, mapping, and restructuring of JSON without writing a full program.

jq is essential for anyone working with APIs, configuration files, or data pipelines in the terminal. Developers, DevOps engineers, and data analysts use it daily to inspect API responses, transform log output, and process CI/CD artifacts.

§02

How it saves time or tokens

Without jq, extracting a field from a JSON API response requires writing a script in Python or Node.js. With jq, a single pipe does the job: curl api | jq '.data.items[].name'. This eliminates the overhead of writing, running, and maintaining throwaway scripts. For AI workflows, jq is invaluable for post-processing LLM API responses -- extracting token counts, filtering results, or reformatting output for downstream tools.

§03

How to use

  1. Install jq: brew install jq (macOS) or sudo apt install jq (Linux).
  2. Pipe JSON into jq: echo '{"name":"Alice"}' | jq .name.
  3. Use filters to extract, transform, or restructure data.
  4. Combine with curl for API inspection: curl -s api.example.com | jq '.results[]'.
§04

Example

# Pretty-print JSON
echo '{"name":"Alice","age":30}' | jq .

# Extract nested fields
curl -s https://api.github.com/repos/jqlang/jq | jq '{name: .name, stars: .stargazers_count}'

# Filter array elements
echo '[{"status":"ok"},{"status":"error"}]' | jq '.[] | select(.status == "error")'

# Transform structure
echo '{"users":[{"name":"A","age":25},{"name":"B","age":30}]}' | jq '.users | map({name, adult: (.age >= 18)})'
§05

Related on TokRepo

§06

Common pitfalls

  • jq expressions use single quotes on Unix shells. On Windows PowerShell, you need double quotes with escaped internals, which can be confusing.
  • The .[] operator iterates array elements but produces multiple outputs, not an array. Wrap in [.[] | ...] to collect results back into an array.
  • jq error messages can be cryptic. When a filter fails silently, add ? (try operator) or use --debug-dump-disasm to trace execution.

常见问题

What is jq used for?+

jq processes JSON data from the command line. Common uses include extracting fields from API responses, transforming data structures, filtering arrays, and pretty-printing JSON output. It integrates with curl, shell scripts, and CI/CD pipelines.

How does jq compare to Python for JSON processing?+

jq is faster for one-off transformations directly in the terminal. Python is better for complex logic, error handling, or when you need to combine JSON processing with other operations. jq excels at pipeline-style data extraction.

Can jq handle large JSON files?+

jq loads the entire JSON into memory by default. For very large files, use the `--stream` flag to process the file incrementally. For multi-gigabyte files, consider alternatives like jaq (a Rust jq clone) or process in chunks.

What version of jq should I use in 2026?+

Use jq 1.7 or later, which added new features like try-catch, SQL-style operators, and improved performance. Earlier versions (1.5, 1.6) are still widely installed on older systems but lack these improvements.

Can jq modify JSON files in place?+

jq does not modify files in place. It reads from stdin or a file, applies transformations, and writes to stdout. To update a file, redirect output to a temporary file and replace the original: `jq '.key = "value"' file.json > tmp && mv tmp file.json`.

引用来源 (3)
  • jq GitHub— jq is a lightweight command-line JSON processor
  • jq Manual— jq manual and filter reference
  • jq Releases— jq 1.7 release with new features

讨论

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

相关资产