jq cheatsheet

Formatting & modes

Produce text formats and understand common jq flags. Explore 4 examples, then try one against the included input or your own data.

jq examples

4 examplesProduce text formats and understand common jq flags.
Formatting & modes

Export rows as CSV

@csv safely quotes array values as a CSV row; raw output removes jq string quotes.

.users[] | [.id, .name, .email] | @csv
Input and expected output
Input
{"users":[{"id":1,"name":"Ada","email":"ada@example.test"},{"id":2,"name":"Grace, G.","email":"grace@example.test"}]}
Expected output
1,"Ada","ada@example.test"
2,"Grace, G.","grace@example.test"
Formatting & modes

Transform a whole text input

Raw whole-text mode passes the editor contents as one jq string.

split("\n") | map(select(length > 0)) | length
Input and expected output
Input
alpha
beta
gamma
Expected output
3
Formatting & modes

Generate data without input

Null-input mode runs the filter once with null and ignores the input editor.

[range(1; 6) | {id: ., square: (. * .)}]
Input and expected output
Input
(ignored)
Expected output
[
  {
    "id": 1,
    "square": 1
  },
  {
    "id": 2,
    "square": 4
  },
  {
    "id": 3,
    "square": 9
  },
  {
    "id": 4,
    "square": 16
  },
  {
    "id": 5,
    "square": 25
  }
]
Formatting & modes

Produce compact, sorted JSON

Compact output writes one JSON value per line; sorted keys make object order predictable.

.
Input and expected output
Input
{"z":1,"a":{"y":2,"b":3}}
Expected output
{"a":{"b":3,"y":2},"z":1}