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.
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.
Frequently Asked Questions
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`.
Citations (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
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.