Skip to content

Instantly share code, notes, and snippets.

@dalssaso
Forked from olih/jq-cheetsheet.md
Created March 22, 2022 22:49
Show Gist options
  • Select an option

  • Save dalssaso/dab4f0e15c2a47d8475fba1839e6f7e5 to your computer and use it in GitHub Desktop.

Select an option

Save dalssaso/dab4f0e15c2a47d8475fba1839e6f7e5 to your computer and use it in GitHub Desktop.
jq Cheet Sheet

Processing JSON with JQ:

| --version| Output the jq version and exit with zero. | |--sort-keys |Output the fields of each object with the keys in sorted order.|

, | If two filters are separated by a comma, then the input will be fed into both and there will be multiple outputs ? | Ignores error if unexpected type [] | Arrya construction {} | Object construction

  • | concatenate or add
  • | difference of set or substract length | size of selected element

From Object

Keys | jq 'keys' Adds + 1 to all items | jq 'map_values(.+1)' Delete key| jq 'del(.foo)'

From Array

|| Action || Command || | All | jq .[] | First | jq '.[0]' Range | jq '.[2:4]' First 3 | jq '.[:3]' Last 2 | jq '.[-2:]' Before Last | jq '.[-2]' Add + 1 to all items | jq 'map(.+1)' Delete 2 items| jq 'del(.[1, 2])' Select array of objects | jq '.[] | select(.id == "second")' Select array of int | jq 'map(select(. >= 2))' Select by type | jq '.[]|numbers' or arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars Concatenate array | jq 'add' Flatten an array | jq 'flatten' Create a range of number | jq '[range(2;4)]' Display type of each item| jq 'map(type)' Sort an array of basic type| jq 'sort' Sorts an array of objects | jq 'sort_by(.foo)' Group by a key - opposite to flatten | jq 'group_by(.foo)' Minimun of an array| jq 'min' .See also min, max, min_by(path_exp), max_by(path_exp) Removes duplicates| jq 'unique' ; jq 'unique_by(.foo)'; jq 'unique_by(length)' Reverse an array | jq 'reverse'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment