jq Cheat Sheet
Practical jq cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
jq terminal commands productivity
jq cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the curl Cheat Sheet and JSON Cheat Sheet.
Setup and Installation
Goal: Verify installation and CLI metadata
# Resolve command path from current shell
command -v jq
# Print installed version to confirm runtime
jq --version
# Read top-level help before using subcommands
jq --help
Goal: Install or upgrade the tool on a workstation
# Install package with Homebrew on macOS
brew install jq
# Upgrade to the latest available package
brew upgrade jq
# Re-check version after upgrade
jq --version
Goal: Run day-to-day commands
# Pretty-print JSON response
cat data.json | jq '.'
# Extract nested field values
cat data.json | jq -r '.items[].name'
# Filter array elements by condition
cat data.json | jq '.items[] | select(.enabled == true)'
# Construct new JSON object
cat data.json | jq '{count: (.items|length), names: [.items[].name]}'
Core Workflows
Goal: Capture command help for quick offline lookup
# Create docs folder for generated command references
mkdir -p docs/cli
# Write help output to a timestamped file
jq --help > docs/cli/jq-help.txt
# Search help output for a keyword
rg "config|auth|deploy" docs/cli/jq-help.txt
Goal: Wrap repetitive commands in a script
# Create scripts directory for local automation
mkdir -p scripts
# Write repeatable health-check script
cat > scripts/check-jq.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
jq --version
jq --help >/dev/null
SH
# Make script executable and run it
chmod +x scripts/check-jq.sh && ./scripts/check-jq.sh
Configuration and Environment
Goal: Pin environment variables for predictable runs
# Define environment profile for local commands
export JQ_PROFILE=dev
# Persist profile in local shell configuration
echo 'export JQ_PROFILE=dev' >> ~/.zshrc
# Reload shell profile changes
source ~/.zshrc
Automation and CI
Goal: Add tool checks to CI pipeline
# Run lint/test/build in strict mode
set -euo pipefail
# Verify CLI exists before invoking workflow
command -v jq
# Exit quickly if command is unavailable
jq --version
Debugging and Troubleshooting
Goal: Collect diagnostics when commands fail
# Capture command output and exit code
jq --help > /tmp/jq-debug.log 2>&1; echo $?
# Inspect captured diagnostics
tail -n 80 /tmp/jq-debug.log
# Check current shell PATH entries
echo $PATH | tr ':' '\n'
Common Gotchas
- Pin jq versions in CI so local and pipeline behavior match.
- Check tool authentication context before running write or deploy commands.
- Prefer non-interactive flags in scripts to avoid stalled jobs.
- Capture stderr logs in CI artifacts for faster incident triage.
- Keep secrets in environment variables, not committed scripts.
Related Sheets
- curl Cheat Sheet — daily curl commands and production-ready examples.
- JSON Cheat Sheet — daily JSON commands and production-ready examples.
- Nix Cheat Sheet — daily Nix commands and production-ready examples.
Related Cheat Sheets
curl Cheat Sheet
Practical curl cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
JSON Cheat Sheet
Practical JSON cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
Nix Cheat Sheet
Practical Nix cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.