-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq_proto.sh
52 lines (31 loc) · 1.85 KB
/
jq_proto.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Prototypical statements for the json-processing command line tool `jq`.
#
# author: andreasl
###############################################################################
# command line args
# jq -r # remove quotation marks from output; don't parse input as json, instead, each line is passed to the filter as a string.
###############################################################################
# treat an array of jsons as several individual jsons
cat myarrayfile.json | jq '.[]' # many json objects, without comma between them
###############################################################################
# output json items in 1 row each
cat myndjsonfile.json | jq -c # output every document on 1 line
###############################################################################
# select stuff and filter
cat myarrayfile.json | jq -r '.[] | .location' # get the location fields without quotation marks
cat myarrayfile.json | jq '.[] | select(.state == "some_state") | "\(.loc), \(.sku)"'
cat myndjsonfile.json | jq 'select(.state == "some_state") | "\(.loc), \(.sku)"' # for newline-delimited json
# more complicated query
cat myndjsonfile.json | jq 'select((.main_image | length > 0) and .location_code != null)'
###############################################################################
# get distinct values
cat myfile.json | jq 'map(.myfield) | unique' # jq-ish way
cat myarrayfile.json | jq '.[] | .state' | sort -u # bash-y way
###############################################################################
# count
# count array lenghts
cat myarrayfile.json | jq 'map(.myfield) | unique | length'
###############################################################################
# grouping
# group all json objects with the same value for `myfield` into arrays, yielding several arrays
cat myarrayfile.json| jq 'group_by(.myfield)'