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
Pretty-print JSON API response
curl -s https://api.github.com/repos/jqlang/jq | jq '.'

# Format a JSON HTTP response from curl.

Extract one field from API response
curl -s https://api.github.com/repos/jqlang/jq | jq -r '.full_name'

# Print a single field like a repo name or count.

List names from API array
curl -s https://api.github.com/orgs/github/repos?per_page=5 | jq -r '.[].full_name'

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

Convert API results to CSV
curl -s https://api.github.com/orgs/github/repos?per_page=5 | jq -r '.[] | [.name, .stargazers_count] | @csv'

# Build CSV rows from JSON array objects.

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

# Return only items matching a specific field value.

## AWS, kubectl, and Docker
Get EC2 instance IDs
aws ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'

# Extract instance IDs from AWS CLI JSON output.

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

# List only instances in the running state.

List Kubernetes pod names
kubectl get pods -o json | jq -r '.items[].metadata.name'

# Extract pod names from kubectl JSON output.

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

# Show only pods in Running phase.

Get Docker container IP
docker inspect my-container | jq -r '.[0].NetworkSettings.IPAddress'

# Extract container IP from `docker inspect`.

List Terraform state resources
terraform show -json | jq -r '.values.root_module.resources[].address'

# Extract resource addresses from Terraform state JSON.

## Logs and Reporting
Extract log levels
cat app.log | jq -r '.level'

# Read a field from each JSON log line.

Filter error logs
cat app.log | jq 'select(.level == "error")'

# Return only error-level JSON log lines.

Count logs by level
cat app.log | jq -s 'group_by(.level) | map({level: .[0].level, count: length})'

# Aggregate log frequency by level.

Turn JSON logs into TSV
cat app.log | jq -r '[.timestamp, .level, .message] | @tsv'

# Extract selected fields into tab-separated text.

Build Markdown table rows
echo '[{"name":"alice","score":90},{"name":"bob","score":85}]' | jq -r '.[] | "| (.name) | (.score) |"'

# Create Markdown-friendly rows from JSON data.

Convert env vars into JSON object
APP_NAME=cheatsheet ENV=prod jq -n --arg app "$APP_NAME" --arg env "$ENV" '{app: $app, env: $env}'

# Build JSON from shell variables.

## Arguments and Shell Integration
Pass string variable
name=alice; jq -n --arg name "$name" '{name: $name}'

# Inject a shell string into a jq program.

Pass JSON value
count=42; jq -n --argjson count "$count" '{count: $count}'

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

Read another JSON file into variable
jq --slurpfile cfg config.json '.items[] | {name, config: $cfg[0]}' data.json

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

Read one JSON file into variable
jq --argfile cfg config.json '{config: $cfg, data: .}' data.json

# Load one JSON document into a jq variable.

Use jq exit status
echo '{"healthy":true}' | jq -e '.healthy' > /dev/null

# Set shell exit code based on jq result.

Recommended next

No recommendations yet.