-
Notifications
You must be signed in to change notification settings - Fork 3
/
collate-keys.jq
67 lines (51 loc) · 1.03 KB
/
collate-keys.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
# 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)
|
# Make another array for the output, then...
[
# 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
)
}
]