json

basic

SYNTAX
DESCRIPTION

,

Filters separated by a comma will produce multiple independent outputs

?

Will ignores error if the type is unexpected

[]

Array construction

{}

Object construction

+

Concatenate or Add

-

Difference of sets or Substract

length

Size of selected element

⎮

Pipes are used to chain commands in a similar fashion than bash

DESCRIPTION
COMMAND

Display all keys

jq 'keys'

Adds + 1 to all items

jq 'map_values(.+1)'

Delete a key

jq 'del(.foo)'

Convert an object to array

to_entries ⎮ map([.key, .value])

[!NOTE|label:references:]

  • select by type: with type been arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars

DESCRIPTION
COMMAND

All

jq .[]

First

jq '.[0]'

Range

jq '.[2:4]'

First 3

jq '.[:3]'

Last 2

jq '.[-2:]'

Before Last

jq '.[-2]'

split

jq '.[] ⎮ split("/")[1]'

Select array of int by value

jq 'map(select(. >= 2))'

Select array of objects by value

jq '.[] ⎮ select(.id == "second")'

Select by type

jq '.[] ⎮ numbers'

DESCRIPTION
COMMAND

Add + 1 to all items

jq 'map(.+1)'

Delete 2 items

jq 'del(.[1, 2])'

Concatenate arrays

jq 'add'

Flatten an array

jq 'flatten'

Create a range of numbers

jq '[range(2;4)]'

Display the type of each item

jq 'map(type)'

Sort an array of basic type

jq 'sort'

Sort an array of objects

jq 'sort_by(.foo)'

Group by a key - opposite to flatten

jq 'group_by(.foo)'

Minimun value of an array

jq 'min' See also min, max, min_by(path_exp), max_by(path_exp)

Remove duplicates

jq 'unique' or jq 'unique_by(.foo)' or jq 'unique_by(length)'

Reverse an array

jq 'reverse'

join

[!TIP] references:

  • or

  • or

  • or with reduce

  • or with .first and .last

  • or

  • or

join with getOrDefault

[!TIP|label:references:]

  • orignal wihtout getOrDefault

split

[!TIP] references:

replacing

[!TIP|label:references:]

  • sub(regex; tostring), sub(regex; string; flags)

    • Emit the string obtained by replacing the first match of regex in the input string with tostring, after interpolation. tostring should be a jq string, and may contain references to named captures. The named captures are, in effect, presented as a JSON object (as constructed by capture) to tostring, so a reference to a captured variable named "x" would take the form: "(.x)".

builtin operators

[!NOTE|label:references:]

debug

  • without debug

  • with debug:

select

[!NOTE|label:refrences:]

contains

[!NOTE|label:reference:]

[!NOTE] references:

  • example to get key and value

    • or output format to @csv

    • or output format to @tsv

  • to_entries and select

from_entries

with_entries

[!NOTE|label:references:]

  • to get particular branch permissions from Gerrit

as

[!NOTE|label:references]

tricky

space in the key

[!TIP|label:references:]

[!NOTE|label:references:]

  • semantics of map on a sequence of objects in jq

    • map( ... ) ≡ [ .[] | ... ], which map( . ) ≡ [ .[] | . ] ≡ [ .[] ]

    • for array: map( . ) ≡ [ .[0], .[1], ... ] ≡ .

    • for object: map( . ) ≡ [ .["key1"], .["key2"], ... ]

Last updated