___________ __________ _____ ________ ________ .____ ___________ \_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/ | __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_ | \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \ \___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ / \/ \/ \/ \/ \/ \/ \/

jq Cheatsheet

← Back to cheatsheets

← Home


jq is a lightweight and flexible command-line JSON processor. Parse, filter, transform, and manipulate JSON data with ease. Essential for API responses, configuration files, and data processing pipelines.


Basic Usage

  • jq '.' file.json - Pretty print JSON
  • jq '.key' file.json - Extract value by key
  • echo '{"key":"value"}' | jq '.' - Pipe JSON
  • jq -r '.key' file.json - Raw output (no quotes)

Filtering

  • jq '.key' - Get value
  • jq '.key1.key2' - Nested access
  • jq '.[]' - Array iteration
  • jq '.[0]' - First element
  • jq '.[-1]' - Last element
  • jq '.[:3]' - First 3 elements
  • jq '.[] | select(.key == "value")' - Filter objects
  • jq '.[] | select(.key > 10)' - Numeric comparison

Object Operations

  • jq '.key1, .key2' - Multiple keys
  • jq '{newkey: .oldkey}' - Rename key
  • jq '. + {newkey: "value"}' - Add key
  • jq 'del(.key)' - Delete key
  • jq 'keys' - List all keys
  • jq 'has("key")' - Check if key exists

Array Operations

  • jq '.array[]' - Iterate array
  • jq '.array | length' - Array length
  • jq '.array | .[0:3]' - Slice array
  • jq '.array + ["new"]' - Append element
  • jq '.array | map(.key)' - Extract key from objects
  • jq '.array | sort' - Sort array
  • jq '.array | unique' - Remove duplicates
  • jq '.array | reverse' - Reverse array

String Operations

  • jq '.key | length' - String length
  • jq '.key | split(",")' - Split string
  • jq '.key | startswith("prefix")' - Check prefix
  • jq '.key | endswith("suffix")' - Check suffix
  • jq '.key | contains("substring")' - Check substring
  • jq '.key | upper' - Uppercase
  • jq '.key | lower' - Lowercase

Output Options

  • -r / --raw-output - Raw string output
  • -c / --compact-output - Compact JSON
  • -s / --slurp - Read entire input as array
  • -n / --null-input - Null input
  • --tab - Use tabs for indentation
  • --indent N - Indentation level

Common Examples

Pretty Print

cat data.json | jq '.'

Format JSON nicely.

Extract Field

jq '.name' users.json

Get name field from objects.

Filter Array

jq '.[] | select(.status == "active")' users.json

Filter objects by condition.

Extract Multiple Fields

jq '{name: .name, id: .id}' users.json

Create new object with selected fields.

Array to CSV

jq -r '.[] | [.name, .email] | @csv' users.json

Convert JSON array to CSV.

Count Elements

jq '.items | length' data.json

Count items in array.

Sum Values

jq '[.[] | .price] | add' items.json

Sum numeric values.

Group By

jq 'group_by(.category) | map({category: .[0].category, count: length})' items.json

Group objects by field.

Flatten Nested

jq '.users[].name' data.json

Extract from nested arrays.

Merge Objects

jq '. + {timestamp: now}' data.json

Add field to object.


Tips

  • Use -r for raw string output in scripts
  • Use .[] to iterate arrays and objects
  • Use select() for filtering
  • Use map() to transform arrays
  • Combine filters with | (pipe)
  • Use @csv, @tsv for formatted output
  • Essential for API response processing
  • Great for configuration file manipulation