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 dry-run、列出写入项,确认后再继续。
npx -y tokrepo@latest install f8b7c935-3712-11f1-9bc6-00163e2b0d79 --target codex先 dry-run,确认写入项后再运行此命令。
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.
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.
How to use
- Install jq:
brew install jq(macOS) orsudo apt install jq(Linux). - Pipe JSON into jq:
echo '{"name":"Alice"}' | jq .name. - Use filters to extract, transform, or restructure data.
- Combine with curl for API inspection:
curl -s api.example.com | jq '.results[]'.
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)})'
Related on TokRepo
- AI tools for automation -- discover command-line tools for automating data workflows.
- Automation tools collection -- browse CLI utilities curated on TokRepo.
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-disasmto trace execution.
常见问题
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.
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.
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.
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.
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
讨论
相关资产
Transfer.sh — Self-Hosted File Sharing from the Command Line
Transfer.sh is a lightweight file sharing service that lets you upload files from the terminal with a single curl command and share them via auto-generated URLs, supporting encryption, expiration, and multiple storage backends.
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.
jnv — Interactive JSON Filter Using jq in Your Terminal
A terminal-based interactive JSON viewer and filter that lets you explore and query JSON data with live jq expression evaluation and instant feedback.
Newman — Run Postman API Collections from the Command Line
Command-line collection runner for Postman that executes API tests in CI/CD pipelines with detailed reporting.