jq CLI Recipes

Real-world jq commands for APIs, cloud tooling, DevOps output, reporting, logs, and shell scripting.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

cURL and API Recipes

Use jq with HTTP APIs and JSON responses.

Pretty-print JSON API response

Format a JSON HTTP response from curl.

bashANYjqcurlapi
bash
curl -s https://api.github.com/repos/jqlang/jq | jq '.'
Notes

A foundational pattern for API inspection.

Extract one field from API response

Print a single field like a repo name or count.

bashANYjqcurlextract
bash
curl -s https://api.github.com/repos/jqlang/jq | jq -r '.full_name'
Notes

Use `-r` for plain shell-friendly output.

List names from API array

Extract one field from each array item in an API response.

bashANYjqcurlarrays
bash
curl -s https://api.github.com/orgs/github/repos?per_page=5 | jq -r '.[].full_name'
Notes

Very common for quick reporting from API arrays.

Convert API results to CSV

Build CSV rows from JSON array objects.

bashANYjqcurlcsv
bash
curl -s https://api.github.com/orgs/github/repos?per_page=5 | jq -r '.[] | [.name, .stargazers_count] | @csv'
Notes

Excellent for spreadsheet exports and ad hoc reports.

Filter repos by language

Return only items matching a specific field value.

bashANYjqcurlfiltering
bash
curl -s https://api.github.com/orgs/github/repos?per_page=20 | jq '.[] | select(.language == "Go") | {name, language}'
Notes

A good example of combining `select()` with object projection.

AWS, kubectl, and Docker

Use jq with common DevOps tools that emit JSON.

Get EC2 instance IDs

Extract instance IDs from AWS CLI JSON output.

bashANYjqawsec2
bash
aws ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'
Notes

This pattern appears constantly in shell automation.

Filter running EC2 instances

List only instances in the running state.

bashANYjqawsfiltering
bash
aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(.State.Name == "running") | {id: .InstanceId, type: .InstanceType}'
Notes

Great for lightweight audits without crafting a JMESPath query.

List Kubernetes pod names

Extract pod names from kubectl JSON output.

bashANYjqkubectlpods
bash
kubectl get pods -o json | jq -r '.items[].metadata.name'
Notes

Perfect for loops and quick visibility.

Filter running pods

Show only pods in Running phase.

bashANYjqkubectlfiltering
bash
kubectl get pods -o json | jq '.items[] | select(.status.phase == "Running") | .metadata.name'
Notes

Useful for triage and operational scripts.

Get Docker container IP

Extract container IP from `docker inspect`.

bashANYjqdockerinspect
bash
docker inspect my-container | jq -r '.[0].NetworkSettings.IPAddress'
Notes

A classic jq + Docker recipe.

List Terraform state resources

Extract resource addresses from Terraform state JSON.

bashANYjqterraformstate
bash
terraform show -json | jq -r '.values.root_module.resources[].address'
Notes

Excellent for inventorying state contents.

Logs and Reporting

Analyze logs and create shell-friendly reports.

Extract log levels

Read a field from each JSON log line.

bashANYjqlogsjson-lines
bash
cat app.log | jq -r '.level'
Notes

Works when each log line is a JSON object.

Filter error logs

Return only error-level JSON log lines.

bashANYjqlogsfiltering
bash
cat app.log | jq 'select(.level == "error")'
Notes

Simple and effective for local debugging.

Count logs by level

Aggregate log frequency by level.

bashANYjqlogsaggregation
bash
cat app.log | jq -s 'group_by(.level) | map({level: .[0].level, count: length})'
Notes

Useful for operational summaries and postmortems.

Turn JSON logs into TSV

Extract selected fields into tab-separated text.

bashANYjqlogstsv
bash
cat app.log | jq -r '[.timestamp, .level, .message] | @tsv'
Notes

Great for quick grep-friendly tabular output.

Build Markdown table rows

Create Markdown-friendly rows from JSON data.

bashANYjqmarkdownreporting
bash
echo '[{"name":"alice","score":90},{"name":"bob","score":85}]' | jq -r '.[] | "| (.name) | (.score) |"'
Notes

Handy for docs, PR comments, and status reports.

Convert env vars into JSON object

Build JSON from shell variables.

bashANYjqshellenv
bash
APP_NAME=cheatsheet ENV=prod jq -n --arg app "$APP_NAME" --arg env "$ENV" '{app: $app, env: $env}'
Notes

A strong pattern for shell scripting and automation.

Arguments and Shell Integration

Pass values from the shell safely into jq.

Pass string variable

Inject a shell string into a jq program.

bashANYjqargsshell
bash
name=alice; jq -n --arg name "$name" '{name: $name}'
Notes

Use `--arg` for shell strings.

Pass JSON value

Inject a JSON literal like a number, array, or object.

bashANYjqargsjson
bash
count=42; jq -n --argjson count "$count" '{count: $count}'
Notes

Use `--argjson` when the value should remain JSON, not a string.

Read another JSON file into variable

Load a JSON file into jq as an array of documents.

bashANYjqslurpfilefiles
bash
jq --slurpfile cfg config.json '.items[] | {name, config: $cfg[0]}' data.json
Notes

Useful for joins between documents or config-driven transforms.

Read one JSON file into variable

Load one JSON document into a jq variable.

bashANYjqargfilefiles
bash
jq --argfile cfg config.json '{config: $cfg, data: .}' data.json
Notes

Use when you need a single parsed JSON document as context.

Use jq exit status

Set shell exit code based on jq result.

bashANYjqexit-statusshell
bash
echo '{"healthy":true}' | jq -e '.healthy' > /dev/null
Notes

`-e` is excellent for shell conditions and CI checks.

Recommended next

No recommendations yet.