Programming · Cheatsheet
jq Commands Cheatsheet
Pretty-print, select, map, and transform JSON on the command line
37 commands 7 sections
No entry matches that filter.
-
jq . file.jsonPretty-print JSON -
jq -c . file.jsonCompact one-line JSON -
jq -r .name file.jsonRaw string without quotes -
jq .user.email file.jsonRead a nested field -
jq .name // "unknown"Default when null or missing -
jq .items[0]First array element -
jq .items[-1]Last array element
-
jq .items[]Stream each array item -
jq .items[1:4]Slice array indexes 1, 2, 3 -
jq keysList object keys -
jq has("email")True if the key exists -
jq lengthLength of array, object, or string -
jq typeJSON type of the input
-
jq ".[] | select(.active)"Keep matching objects -
jq "map(.email)"Project one field into an array -
jq "map(select(.score > 50))"Filter then keep an array -
jq ".[] | select(has("email"))"Keep objects with a key -
jq "select(.role == "admin")"Match a string field
-
jq "{name, id}"Build a smaller object -
jq "del(.password)"Remove a field -
jq ".count += 1"Update a number field -
jq "to_entries"Object to key/value array -
jq "from_entries"Key/value array to object -
jq "flatten"Flatten nested arrays
-
jq "sort_by(.name)"Sort objects by a field -
jq "unique_by(.email)"Deduplicate by a field -
jq "group_by(.team)"Bucket objects by a field -
jq "map(.price) | add"Sum a numeric field -
jq "min_by(.age)"Object with the lowest field -
jq "max_by(.score)"Object with the highest field
-
jq -r ".name | ascii_downcase"Lowercase a string -
jq -r "split("/")"Split a string into an array -
jq -r "join(",")"Join an array into a string -
jq "select(.msg | test("error"; "i"))"Regex match, ignore case -
curl URL | jq .Pretty-print an HTTP JSON body -
jq -n --arg n "$NAME" '{name:$n}'Build JSON from a shell variable
-
https://jqlang.org/manual/jq manual: command-line filters and options. Browser playgrounds can support a smaller subset.
Related cheatsheets
Guides that use these commands
Tools for this work
- JSON YAML ConverterConvert between JSON and YAML formats instantly with syntax validation.
- JSON to TypeScriptConvert JSON to TypeScript interfaces or type aliases with customizable options.
- JSON to LiquidTurn JSON, JS, Python, PHP or YAML data, or a Handlebars, Mustache or Knap template, into Shopify Liquid.