-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollate-keys-index.jq
99 lines (76 loc) · 1.32 KB
/
collate-keys-index.jq
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# This script take a JSON and walks through it to find all key value pairs where the value is a scalar or null.
# It then makes a dictionary of all the unique values in the JSON for each of those keys.
# This function gets an element if it is a leaf node
def grab(d):
d
|
to_entries?
|
.[]
|
select(
.value
|
type != "array" and type != "object"
);
# Make an array
[
# Crawl through the JSON
recurse
|
# Find all the leaf nodes
grab(.)
|
# Ignore scalars that are in arrays (we are only focusing on key value pairs in the JSON)
select(.key | type == "string")
]
|
# Now take the array and collate it by key
group_by(.key)
|
[
# for each different key...
.[]
|
# make an object...
{
# that has the key name as the key...
(.[0].key):
(
# and all the unique values in an array attached to that key.
[
.[]
|
.value
]
|
unique
)
}
]
|
# Put it all into one JSON
add
|
# Prepare to create a nice output table
. as $original
|
# For each key get the length of the list of unique items in that category.
keys
|
length as $len
|
# Now, from the top...
$original
|
# ...iterate through the list of keys with an index number.
range($len) as $index
|
keys[$index] as $key
|
.[$key]
|
# Emit table row
[$index, $key, length]
|
@csv