Skip to content

Snippets

nntrn edited this page Sep 29, 2023 · 2 revisions

https://nntrn.github.io/jq-recipes


timestamp

$ jq -Rnsc '{ "@timestamp": now|strftime("%Y-%m-%dT%H:%M:%S%z") }'
{"@timestamp":"2022-11-06T07:44:09+0000"}

interpreter

$ echo '"Hello, World!"' > hello.jq

$ jq -nr -f  hello.jq '.'
Hello, World!

command line program

$ echo '[1,2,3]' | jq -c 'map(. * .)'
[1,4,9]

$ jq -c -n '"1 2 3" | split(" ") | map(tonumber)| map(. * .)'
[1,4,9]

compound assignment

|= += -= *= /= %= //=

assignment

$ echo '[1,2,3]' | jq -c 'length as $x | map(. * $x)'
[3,6,9]

$ echo '{"t": 1, "f": 0}' | jq -c '.t += 1'
{"t":2,"f":0}

More than one field can be updated.

$ echo '{"t": 1, "f": 0}' | jq -c '.[] += 1'
{"t":2,"f":1}

null test

$ echo '{"name": "Ed"}' | jq '.age == null'
true

coalesce

$ echo '{}' | jq '.age // 0'
0

conditional

$ echo '-3' | jq 'if . < 0 then -. else . end'
3

min

$ echo '[1,2,3]' | jq 'min'
1

max

$ echo '[1,2,3]' | jq 'max'
3

floor

$ jq -n '-13 / 5 | floor'
-3

$ jq -n '1.1 | floor'
1

sqrt

$ jq -n '2 | sqrt'
1.4142135623730951

sqrt -1

$ jq -n '-1 | sqrt'
null

variable interpolation

$ echo '{"foo": 1, "bar": 2}' | jq '"\(.foo):\(.bar)"'
"1:2"

ascii_upcase

$ jq -n '"foo" | ascii_upcase'
"FOO"

ascii_downcase

$ jq -n '"FOO" | ascii_downcase'
"foo"

tostring

$ echo '7' | jq 'tostring'
"7"

tonumber

$ echo '7' | jq 'tonumber'
7

split

$ echo '"do re mi"' | jq -c 'split(" ")'
["do","re","mi"]

trim/split

$ echo '"--do-re-mi"' | jq -c 'split("-") | map(select(length > 0))'
["do","re","mi"]

startswith

$ echo '"foobar"' | jq 'startswith("foo")'
true

endswith

$ echo '"foobar"' | jq 'endswith("bar")'
true

index/rindex

$ echo '"do re re"' | jq 'index("re")'
3

$ echo '"do re re"' | jq 'rindex("re")'
6

$ echo '"do re re"' | jq 'rindex("mi")'
null

test

$ echo $'"It\'s 1999!"' | jq '. | test("1999")'
true

$ echo $'"FOO BAR"' | jq 'test("foo"; "i")'
true

$ echo $'"FOO BAR"' | jq 'test(["foo", "i"])'
true

$ echo $'"FOO BAR"' | jq 'test("(?i)foo")'
true

$ echo $'"FOO BAR"' | jq 'test("foo")'
false

sub/gsub

$ echo '"do re mi mi mi"' | jq 'sub("mi"; "ma")'
$ echo '"do re mi mi mi"' | jq 'gsub("mi"; "ma")'

capture

$ echo '"foo.txt"' | jq 'capture("^(?<root>.*)\\.(?<suffix>.*)$") | .root'

date/time

$ jq -n 'now | gmtime'

$ jq -n 'now | gmtime | todate'

current unix epoch

$ jq -n 'now'

broken-down datetime to unix epoch

$ echo '[2016,11,15,11,30,0,4,349]' | jq 'mktime'

unix epoch to broken-down datetime

$ echo 1481801400 | jq 'gmtime'

format datetime

$ jq -n 'now | gmtime | strftime("%Y-%m-%d %H:%M:%S")'

slice from beginning

$ jq -c '.[:2]'

from element 3 on:

$ jq -c '.[2:]'

last two elements:

$ jq -c '.[-2:]'

indices

$ jq -c 'keys'

reverse

$ echo '[1,2,3]' | jq 'reverse'

sort

$ echo '[3,1,4,2]' | jq 'sort'

dedupe

$ echo '[1,1,2,3]' | jq 'unique'

subset test

$ echo '[1,2,3]' | jq 'contains([1])'
$ echo '[1]' | jq 'inside([1,2,3])'

map

$ echo '[1,2,3]' | jq '.[] | . * .'
$ echo '[1,2,3]' | jq 'map(. * .)'

filter

$ echo '[1,2,3]' | jq 'map(select(. > 2))'

reduce

$ echo '[1,2,3,4]'| jq 'reduce .[] as $item (0; .+$item)'
10

all/any

$ echo '[1,2,3]' | jq 'all(. > 2)'
$ echo '[1,2,3]' | jq 'any(. > 2)'

flatten

$ echo '[1,[2,[3]]]' | jq -c 'flatten(1)'
$ echo '[1,[2,[3]]]' | jq -c 'flatten'

lookup

$ echo '{"t": 1, "f": 0}' | jq '.t'
$ echo '{"t": 1, "f": 0}' | jq '.["t"]'

update

$ echo '{"t": 1, "f": 0}' | jq '.f = -1'

is key present

$ echo '{"t": 1, "f": 0}' | jq 'has("t")'
$ echo '"t"' | jq 'in({"t": 1, "f": 0})'

delete

$ echo '{"t": 1, "f": 0}' | jq 'del(.t)'

keys and values as arrays

$ echo '{"do": 1, "re": 2}' | jq 'keys'
[
  "do",
  "re"
]

$ echo '{"do": 1, "re": 2}' | jq 'to_entries | map(.value)'
[
  {
    "key": "do",
    "value": 1
  },
  {
    "key": "re",
    "value": 2
  }
]

define

$ echo '1234' | jq 'def double: 2 * .;'

call

$  echo '1 2 3 4' | jq 'def double: 2 * .; double'

if

$ echo '-3' | jq 'if . < 0 then -. else . end'

while

$ jq -n '1 | while(. < 10; . + 1)'
1
2
3
4
5
6
7
8
9

break

$ jq -n '1 | label $out | while(true; if . > 10 then break $out else. + 1 end)'

write to stderr

$ jq -n '"foo" | debug'

read input as array of strings

$ jq -R 'length' /etc/hosts

array to stream

$ echo '[1,2,3]' | jq '.[]'

stream to array

$ echo '123' | jq -s '.'

environment variable

$ jq -n 'env.HOME'
$ jq -n 'env["HOME"]'

load library

$ echo -e 'def double: 2 * .;\ndef triple: 3 * .;\ndef quadruple; 4 * .;' > multiples.jq

$ echo 3 | jq 'include "multiples"; double'

library path command line option

$ mkdir ~/.jq
$ echo -e 'def double: 2 * .;\ndef triple: 3 * .;\ndef quadruple; 4 * .;' > ~/.jq/multiples.jq
$ cat ~/.jq/multiples.jq
def double: 2 * .;
def triple: 3 * .;
def quadruple; 4 * .;

$ echo 3 | jq -L ~/.jq 'include "multiples"; double'

load definitions in namespace

$ echo -e 'def double: 2 * .;\ndef triple: 3 * .;\ndef quadruple; 4 * .;' > multiples.jq
$ cat multiples.jq
def double: 2 * .;
def triple: 3 * .;
def quadruple; 4 * .;

$ echo 3 | jq 'import "multiples" as mult; mult::double'

inspect type

$ echo '{"foo": 1}' | jq '.foo | type'
Clone this wiki locally