Loading...
Cheat sheet
JSON slicing, filtering, mapping, and kubectl output processing.
# Identity (pretty-print JSON)
cat file.json | jq '.'
# Access a field
jq '.name'
# Nested field
jq '.metadata.name'
# Array index
jq '.[0]'
# Array slice
jq '.[2:5]'
# All array elements
jq '.[]'
# Multiple fields (object construction)
jq '{name: .name, age: .age}'# Map over array
jq '[.[] | .name]'
# Filter array (select)
jq '[.[] | select(.status == "Running")]'
# Select with regex
jq '[.[] | select(.name | test("^web-"))]'
# Flatten nested array
jq '[.[] | .tags[]]'
# Sort by field
jq 'sort_by(.age)'
# Unique values
jq '[.[].status] | unique'
# Length of array
jq '.items | length'
# Group by field
jq 'group_by(.namespace)'# String interpolation
jq '"Hello, \(.name)!"'
# Convert to string
jq '.port | tostring'
# Convert to number
jq '.replicas | tonumber'
# Keys of an object
jq 'keys'
# Values of an object (as array)
jq '[.[]]'
# Check if field exists
jq 'has("metadata")'
# Null-safe access
jq '.labels? // {}'
# Default value
jq '.timeout // 30'# List all pod names
kubectl get pods -o json \
| jq -r '.items[].metadata.name'
# Get pod names and their node
kubectl get pods -o json \
| jq -r '.items[] | "\(.metadata.name) -> \(.spec.nodeName)"'
# Get all non-running pods
kubectl get pods -A -o json \
| jq -r '.items[] | select(.status.phase != "Running") | "\(.metadata.namespace)/\(.metadata.name)"'
# Get container image versions
kubectl get pods -o json \
| jq -r '.items[].spec.containers[] | "\(.name): \(.image)"'
# Find pods with high restart count
kubectl get pods -A -o json \
| jq -r '.items[] | select(.status.containerStatuses[]?.restartCount > 5) | .metadata.name'# Sum values
jq '[.[].count] | add'
# Min / max
jq '[.[].cpu] | min'
jq '[.[].cpu] | max'
# Count items matching condition
jq '[.[] | select(.ready == true)] | length'
# Reduce with accumulator
jq 'reduce .[] as $item (0; . + $item.value)'
# Build lookup map from array
jq 'INDEX(.[]; .id)'# Raw string output (no quotes)
jq -r '.name'
# Compact output (no whitespace)
jq -c '.'
# Null input (generate data)
jq -n '{version: "1.0", env: "prod"}'
# Read from file
jq '.' config.json
# Multiple input files
jq -s '.[0] * .[1]' base.json override.json
# Tab-separated output
jq -r '[.name, .namespace, .status] | @tsv'
# CSV output
jq -r '[.name, .age] | @csv'