---
title: "jq Commands Cheatsheet | FindUtils"
description: "Pretty-print, select, map, and transform JSON on the command line"
url: https://findutils.com/cheatsheets/jq-commands/
---

# jq Commands Cheatsheet

Pretty-print, select, map, and transform JSON on the command line 37 commands in 7 sections, filed under [Programming](https://findutils.com/cheatsheets/programming/).

## Basics

- `jq . file.json`: Pretty-print JSON
- `jq -c . file.json`: Compact one-line JSON
- `jq -r .name file.json`: Raw string without quotes
- `jq .user.email file.json`: Read a nested field
- `jq .name // "unknown"`: Default when null or missing
- `jq .items[0]`: First array element
- `jq .items[-1]`: Last array element

## Arrays and objects

- `jq .items[]`: Stream each array item
- `jq .items[1:4]`: Slice array indexes 1, 2, 3
- `jq keys`: List object keys
- `jq has("email")`: True if the key exists
- `jq length`: Length of array, object, or string
- `jq type`: JSON type of the input

## select and map

- `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

## Transform

- `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

## Sort and aggregate

- `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

## Strings and CLI

- `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

## Sources

- `https://jqlang.org/manual/`: jq manual: command-line filters and options. Browser playgrounds can support a smaller subset.

## Related

- Cheatsheet: [Barcode Formats](https://findutils.com/cheatsheets/barcode-formats/)
- Cheatsheet: [JavaScript ES6+](https://findutils.com/cheatsheets/javascript-es6/)
- Cheatsheet: [Python Essentials](https://findutils.com/cheatsheets/python-essentials/)
- Guide: [JQ Playground Online: How to Query & Transform JSON with jq Filters](https://findutils.com/guides/jq-playground-guide/)
- Guide: [Convert SQL INSERT Statements to JSON With the Right Types](https://findutils.com/guides/sql-to-json/)
- Guide: [How to Convert a Plist File to JSON (XML and Binary)](https://findutils.com/guides/plist-to-json/)
- Tool: [JSON YAML Converter](https://findutils.com/developers/json-yaml-converter/)
- Tool: [JSON to TypeScript](https://findutils.com/developers/json-to-typescript/)
- Tool: [JSON to Liquid](https://findutils.com/developers/json-to-liquid/)
