-
Notifications
You must be signed in to change notification settings - Fork 3
/
.jq
63 lines (51 loc) · 1.27 KB
/
.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
# jq functions
# return value only if not null (update only)
def replace(value):
if . == null then null else value end;
# remove nulls
def denull:
del(.[] | nulls);
# remove placeholder
def remove_placeholders:
[
. | to_entries[] |
select(("<" + (.key | ascii_upcase) + ">") != .value)
] | from_entries;
def case_mod_inner(case_mod_function):
walk(
if type == "object" then
with_entries(.key |= (.[0:1] | case_mod_function) + .[1:])
else
.
end
);
# camelize keys
def camelize:
case_mod_inner(ascii_downcase);
# pascalize keys
def pascalize:
case_mod_inner(ascii_upcase);
# ref: https://stackoverflow.com/a/34282594/58678
def toxsv(transform_function):
if length == 0 then empty
else
(.[0] | keys_unsorted) as $keys
| (map(keys) | add | unique) as $allkeys
| ($keys + ($allkeys - $keys)) as $cols
| ($cols, (.[] as $row | $cols | map($row[.])))
| transform_function
end;
# csv
def tocsv:
toxsv(@csv);
# tsv
def totsv:
toxsv(@tsv);
# unflatten
# see https://stackoverflow.com/questions/57154970/how-to-get-inverse-operation-of-flatten-for-objects-using-jq/57155523#57155523
def un_flatten(delim):
reduce to_entries[] as $kv ({}; setpath($kv.key|split(delim); $kv.value));
def unflatten:
un_flatten(".");
def unflatten(delim):
un_flatten(delim);