-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbasic.sql
177 lines (144 loc) · 5.4 KB
/
basic.sql
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* --- Type creation and casts --- */
select 'gsdfgf'::jqprog;
select 1345::jqprog;
/* --- Arrays as input --- */
select jq('[{"bar": "baz", "balance": 7.77, "active":false}]'::jsonb, '.[0].bar');
select jq('[{"bar": "baz", "balance": 7.77, "active":false}]'::jsonb, '.[0].balance');
select jq('[{"bar": "baz", "balance": 7.77, "balance_int": 7, "active":false}]'::jsonb, '.[0].balance_int');
select jq('[{"bar": "baz", "balance": 7.77, "active":false}]'::jsonb, '.[0].active');
select jq('[{"bar": "baz", "balance": 7.77, "active":false, "keno": null}]'::jsonb, '.[0].keno');
select jq('[{"bar": "baz", "balance": 7.77, "active":false, "keno": null}]'::jsonb, '.[0].keno');
select jq('[["a", "b", "c"]]'::jsonb, '.[0]');
select jq('[["a", "b", "c"]]'::jsonb, '.[0].[0]');
select jq('[["a", "b", "c"]]'::jsonb, '.[0].[1]');
select jq('[["a", "b", "c"]]'::jsonb, '.[0].[1000]');
select jq('[{"bar": "baz", "balance": 7.77, "active":false}]'::jsonb, '.[0]');
select jq('[{"bar": "baz", "balance": 7.77, "active":false}]'::jsonb, '.[100]');
select jq('[{"bar": "baz", "balance": 7.77, "active":false}]'::jsonb, '.[0].bar');
select jq('[{"bar": "baz", "balance": 7.77, "active":false}]'::jsonb, '.[0].gdfgdf');
/* --- Single object as input --- */
select jq('{"bar": "baz", "balance": 7.77, "active":false}'::jsonb, '.');
select jq('{"bar": "baz", "balance": 7.77, "active":false}'::jsonb, '.baz');
select jq('{"bar": "baz", "balance": 7.77, "active":false}'::jsonb, '.balance');
select jq('{"bar": "baz", "balance": 7.77, "active":false}'::jsonb, '.gdfgfd');
select jq('{"user":"stedolan", "projects": ["jq", "wikiflow"]}', '.user, .projects[]');
/* --- Nested object --- */
select jq('{"bar": "baz", "balance": 7.77, "active":false, "inner": {"key": "value"}}'::jsonb, '.gdfgfd');
select jq('{"bar": "baz", "balance": 7.77, "active":false, "inner": {"key": "value"}}'::jsonb, '.inner');
select jq('{"bar": "baz", "balance": 7.77, "active":false, "inner": {"key": "value"}}'::jsonb, '.inner.key');
select jq('{"bar": "baz", "balance": 7.77, "active":false, "inner": {"key": "value"}}'::jsonb, '.gdfgfd');
/* --- pick --- */
select jq('{"a": [123]}', '.a[]');
/* --- assignment --- */
select jq('[true,false,[5,true,[true,[false]],false]]', '(..|select(type=="boolean")) |= if . then 1 else 0 end');
select jq('{"foo": 42}', '.foo += 1');
select jq('{"a": {"b": 10}, "b": 20}', '.a = .b');
/* --- select --- */
select jq('[1,5,3,0,7]' , '(.[] | select(. >= 2)) |= empty');
select jq('[1,5,3,0,7]', '.[] |= select(. >= 4)');
select jq('[1,5,3,0,7]', '.[] |= select(. == 2)');
/* --- if/else conditionals --- */
select jq('2', '.<5');
select jq('{"prod": "life", "price": 10}', 'if .price > 0 then "it is expensive" else {"oh": "well"} end');
select jq('{"prod": "life", "price": 10}', 'if .price > 100 then "it is expensive" else {"oh": "well"} end');
select jq('2', 'if . == 0 then "zero" elif . == 1 then "one" else "many" end');
select jq('3', 'if . == 0 then "zero" elif . == 1 then "one" else {"big": "object"} end');
/* --- builtin functions --- */
select jq('1', '[repeat(.*2, error)?]');
select jq('4', '[.,1]|until(.[0] < 1; [.[0] - 1, .[1] * .[0]])|.[1]');
select jq('[[4, 1, 7], [8, 5, 2], [3, 6, 9]]', 'walk(if type == "array" then sort else . end)');
select jq('[ { "_a": { "__b": 2 } } ]', 'walk( if type == "object" then with_entries( .key |= sub( "^_+"; "") ) else . end )');
select jq('[0,1]', 'bsearch(0)');
select jq('[1,2,3]', 'bsearch(0)');
select jq('[1,2,3]', 'bsearch(0)');
select jq('[1,2,3]', 'bsearch(4) as $ix | if $ix < 0 then .[-(1+$ix)] = 4 else . end');
/* --- convert to/from JSON --- */
select jq('[1, "foo", ["foo"]]', '[.[]|tostring]');
select jq('[1, "foo", ["foo"]]', '[.[]|tojson]');
select jq('[1,"foo",["foo"]]', '[.[]|tojson|fromjson]');
/* --- chain with jsonpath --- */
select jq('[
{
"cust": "baz",
"trans": {
"balance": 100,
"date": "2023-08-01"
},
"active": true,
"geo": {
"branch": "paloukia"
}
}]',
'(.[] | select(.active == true))') #- '{trans}' @> '{"cust": "baz"}';
/* --- ---argjson --- */
-- test numeric argument
select jq('{
"success": true,
"message": "jobStatus",
"jobStatus": [
{
"ID": 9,
"status": "Successful"
},
{
"ID": 2,
"status": "Successful"
},
{
"ID": 99,
"status": "Failed"
}
]
}'::jsonb, '.jobStatus[] | select(.ID == $job_id) | .status', '{"job_id": 2}');
-- test string argument
select jq('{
"success": true,
"message": "jobStatus",
"jobStatus": [
{
"ID": 9,
"status": "Successful"
},
{
"ID": 2,
"status": "Successful"
},
{
"ID": 99,
"status": "Failed"
}
]
}'::jsonb, '.jobStatus[] | select(.status == $status) | .', '{"status": "Failed"}');
-- test boolean argument
select jq('{
"success": true,
"message": "jobStatus",
"jobs": [
{
"ID": 9,
"is_successfull": true
},
{
"ID": 100,
"is_successfull": false
}
]
}'::jsonb, '.jobs[] | select(.is_successfull == $is_success) | .', '{"is_success": true}');
-- test multiple arguments
select jq('{
"runner": 1,
"message": "jobStatus",
"jobs": [
{
"id": 9,
"is_successfull": true
},
{
"id": 100,
"is_successfull": false,
"metdata": {
"environ": "prod"
}
}
]
}'::jsonb, '.jobs[] | select(.is_successfull == $is_success and .id == 100) | .', '{"is_success": false, "id": 100}');