jq Cheats
Cheat sheet for jq commands.
info
This page was automatically generated from a navi cheat file available at difranca/navi-cheats.
Navi is an interactive cheatsheet tool for the command-line. To learn more about it, visit denisidoro/navi.
- Table Formatted
- Navi Cheat
Basicsโ
Select and extract data from JSON.
| Command | Description |
|---|---|
| ** | jq '.'** |
| ** | jq '.{field}'** |
| ** | jq '.{field}.{nested_field}'** |
| ** | jq '.[0]'** |
| ** | jq '.[].{field}'** |
| ** | jq '. |
Filter & Selectโ
Filter and search JSON data.
| Command | Description |
|---|---|
| ** | jq '.[] |
| ** | jq '.[] |
| ** | jq '.[] |
| ** | jq '[.[].{field}] |
| ** | jq 'sort_by(.{field})'** |
Transformโ
Reshape and transform JSON.
| Command | Description |
|---|---|
| ** | jq '.[] |
| ** | jq '[.[] |
| ** | jq 'flatten'** |
| ** | jq 'group_by(.{field})'** |
| ** | jq '.[] |
| ** | jq -r '.[] |
| ** | jq '.[] + {"{new_key}": "{value}"}'** |
Outputโ
Control output format.
| Command | Description |
|---|---|
| ** | jq -r '.{field}'** |
| ** | jq -c '.'** |
| ** | jq -r '.[] |
| ** | jq '[.[] |
% jq
;; Cheat sheet for jq commands.
% jq > Basics
;; Select and extract data from JSON.
# Pretty-print JSON
| jq '.'
# Get a top-level field
| jq '.<field>'
# Get a nested field
| jq '.<field>.<nested_field>'
# Get the first element of an array
| jq '.[0]'
# Get a field from each element in an array
| jq '.[].<field>'
# Get array length
| jq '. | length'
$ json_file: find . -name '*.json' | head -20
% jq > Filter & Select
;; Filter and search JSON data.
# Filter array elements by condition
| jq '.[] | select(.<field> == "<value>")'
# Filter elements where field contains a string
| jq '.[] | select(.<field> | contains("<value>"))'
# Filter elements where field is not null
| jq '.[] | select(.<field> != null)'
# Get unique values of a field
| jq '[.[].<field>] | unique'
# Sort array by a field
| jq 'sort_by(.<field>)'
% jq > Transform
;; Reshape and transform JSON.
# Create a new object from fields
| jq '.[] | {<new_key>: .<field>}'
# Map array elements
| jq '[.[] | .<field>]'
# Flatten nested arrays
| jq 'flatten'
# Group by a field
| jq 'group_by(.<field>)'
# Join array elements as string
| jq '.[] | join(",")'
# Convert to CSV-like output
| jq -r '.[] | [.<field1>, .<field2>] | @csv'
# Add a new field to each element
| jq '.[] + {"<new_key>": "<value>"}'
% jq > Output
;; Control output format.
# Output raw strings without quotes
| jq -r '.<field>'
# Compact output (no pretty-print)
| jq -c '.'
# Output as tab-separated values
| jq -r '.[] | [.<field1>, .<field2>] | @tsv'
# Count elements matching a condition
| jq '[.[] | select(.<field> == "<value>")] | length'