1
1
# Advanced JSON
2
+
2
3
Redis JSON array filtering examples
4
+
3
5
## Contents
4
- 1 . [ Business Value Statement] ( #value )
5
- 2 . [ Data Set] ( #dataset )
6
- 3 . [ Data Loading] ( #dataload )
7
- 4 . [ Array Filtering Examples] ( #arrayfiltering )
8
- 1 . [ All Properties of Array] ( #allprops )
9
- 2 . [ All Properties of a Field] ( #allfield )
10
- 3 . [ Relational - Equality] ( #equality )
11
- 4 . [ Relational - Less Than] ( #lessthan )
12
- 5 . [ Relational - Greater Than or Equal] ( #greaterthan )
13
- 6 . [ Logical AND] ( #logicaland )
14
- 7 . [ Logical OR] ( #logicalor )
15
- 8 . [ Regex - Contains Exact] ( #regex_exact )
16
- 9 . [ Regex - Contains, Case Insensitive] ( #regex_contains )
17
- 10 . [ Regex - Begins With] ( #regex_begins )
6
+
7
+ 1 . [ Business Value Statement] ( #value )
8
+ 2 . [ Data Set] ( #dataset )
9
+ 3 . [ Data Loading] ( #dataload )
10
+ 4 . [ Array Filtering Examples] ( #arrayfiltering )
11
+ 1 . [ All Properties of Array] ( #allprops )
12
+ 2 . [ All Properties of a Field] ( #allfield )
13
+ 3 . [ Relational - Equality] ( #equality )
14
+ 4 . [ Relational - Less Than] ( #lessthan )
15
+ 5 . [ Relational - Greater Than or Equal] ( #greaterthan )
16
+ 6 . [ Logical AND] ( #logicaland )
17
+ 7 . [ Logical OR] ( #logicalor )
18
+ 8 . [ Regex - Contains Exact] ( #regex_exact )
19
+ 9 . [ Regex - Contains, Case Insensitive] ( #regex_contains )
20
+ 10 . [ Regex - Begins With] ( #regex_begins )
18
21
19
22
## Business Value Statement <a name =" value " ></a >
23
+
20
24
The ability to query within a JSON object unlocks further value to the underlying data. Redis supports JSONPath array filtering natively.
25
+
21
26
## Data Set <a name =" dataset " ></a >
27
+
22
28
``` JSON
23
29
{
24
30
"city" : " Boston" ,
@@ -48,7 +54,9 @@ The ability to query within a JSON object unlocks further value to the underlyin
48
54
]
49
55
}
50
56
```
57
+
51
58
## Data Loading <a name =" dataload " ></a >
59
+
52
60
``` c#
53
61
JsonCommands json = db .JSON ();
54
62
json .Set (" warehouse:1" , " $" , new {
@@ -79,21 +87,29 @@ json.Set("warehouse:1", "$", new {
79
87
}
80
88
});
81
89
```
90
+
82
91
## Array Filtering Examples <a name =" arrayfiltering " ></a >
92
+
83
93
### Syntax
94
+
84
95
[ JSON.GET] ( https://redis.io/commands/json.get/ )
85
96
86
97
### All Properties of Array <a name =" allprops " ></a >
98
+
87
99
Fetch all properties of an array.
100
+
88
101
#### Command
102
+
89
103
``` c#
90
104
Console .WriteLine (json .Get (key : " warehouse:1" ,
91
105
path : " $.inventory[*]" ,
92
106
indent : " \t " ,
93
107
newLine : " \n "
94
108
));
95
109
```
110
+
96
111
#### Result
112
+
97
113
``` json
98
114
[
99
115
{
@@ -131,16 +147,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
131
147
```
132
148
133
149
### All Properties of a Field <a name =" allfield " ></a >
150
+
134
151
Fetch all values of a field within an array.
152
+
135
153
#### Command
154
+
136
155
``` c#
137
156
Console .WriteLine (json .Get (key : " warehouse:1" ,
138
157
path : " $.inventory[*].price" ,
139
158
indent : " \t " ,
140
159
newLine : " \n "
141
160
));
142
161
```
162
+
143
163
#### Result
164
+
144
165
``` json
145
166
[
146
167
34.95 ,
@@ -150,16 +171,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
150
171
```
151
172
152
173
### Relational - Equality <a name =" equality " ></a >
174
+
153
175
Fetch all items within an array where a text field matches a given value.
176
+
154
177
#### Command
178
+
155
179
``` c#
156
180
Console .WriteLine (json .Get (key : " warehouse:1" ,
157
181
path : " $.inventory[?(@.description==\" Turtle Check Men Navy Blue Shirt\" )]" ,
158
182
indent : " \t " ,
159
183
newLine : " \n "
160
184
));
161
185
```
186
+
162
187
#### Result
188
+
163
189
``` json
164
190
[
165
191
{
@@ -176,16 +202,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
176
202
```
177
203
178
204
### Relational - Less Than <a name =" lessthan " ></a >
205
+
179
206
Fetch all items within an array where a numeric field is less than a given value.
207
+
180
208
#### Command
209
+
181
210
``` c#
182
211
Console .WriteLine (json .Get (key : " warehouse:1" ,
183
212
path : " $.inventory[?(@.price<100)]" ,
184
213
indent : " \t " ,
185
214
newLine : " \n "
186
215
));
187
216
```
217
+
188
218
#### Result
219
+
189
220
``` json
190
221
[
191
222
{
@@ -211,16 +242,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
211
242
```
212
243
213
244
### Relational - Greater Than or Equal <a name =" greaterthan " ></a >
245
+
214
246
Fetch all items within an array where a numeric field is greater than or equal to a given value.
247
+
215
248
#### Command
249
+
216
250
``` c#
217
251
Console .WriteLine (json .Get (key : " warehouse:1" ,
218
252
path : " $.inventory[?(@.id>=20000)]" ,
219
253
indent : " \t " ,
220
254
newLine : " \n "
221
255
));
222
256
```
257
+
223
258
#### Result
259
+
224
260
``` json
225
261
[
226
262
{
@@ -248,16 +284,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
248
284
```
249
285
250
286
### Logical AND <a name =" logicaland " ></a >
287
+
251
288
Fetch all items within an array that meet two relational operations.
289
+
252
290
#### Command
291
+
253
292
``` c#
254
293
Console .WriteLine (json .Get (key : " warehouse:1" ,
255
294
path : " $.inventory[?(@.gender==\" Men\" &&@.price>20)]" ,
256
295
indent : " \t " ,
257
296
newLine : " \n "
258
297
));
259
298
```
299
+
260
300
#### Result
301
+
261
302
``` json
262
303
[
263
304
{
@@ -274,16 +315,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
274
315
```
275
316
276
317
### Logical OR <a name =" logicalor " ></a >
318
+
277
319
Fetch all items within an array that meet at least one relational operation. In this case, return only the ids of those items.
320
+
278
321
#### Command
322
+
279
323
``` c#
280
324
Console .WriteLine (json .Get (key : " warehouse:1" ,
281
325
path : " $.inventory[?(@.price<100||@.gender==\" Women\" )].id" ,
282
326
indent : " \t " ,
283
327
newLine : " \n "
284
328
));
285
329
```
330
+
286
331
#### Result
332
+
287
333
``` json
288
334
[
289
335
15970 ,
@@ -293,16 +339,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
293
339
```
294
340
295
341
### Regex - Contains Exact <a name =" regex_exact " ></a >
342
+
296
343
Fetch all items within an array that match a given regex pattern.
344
+
297
345
#### Command
346
+
298
347
``` c#
299
348
Console .WriteLine (json .Get (key : " warehouse:1" ,
300
349
path : " $.inventory[?(@.description =~ \" Blue\" )]" ,
301
350
indent : " \t " ,
302
351
newLine : " \n "
303
352
));
304
353
```
354
+
305
355
#### Result
356
+
306
357
``` json
307
358
[
308
359
{
@@ -328,16 +379,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
328
379
```
329
380
330
381
### Regex - Contains, Case Insensitive <a name =" regex_contains " ></a >
382
+
331
383
Fetch all items within an array where a field contains a term, case insensitive.
384
+
332
385
#### Command
386
+
333
387
``` c#
334
388
Console .WriteLine (json .Get (key : " warehouse:1" ,
335
389
path : " $.inventory[?(@.description =~ \" (?i)watch\" )]" ,
336
390
indent : " \t " ,
337
391
newLine : " \n "
338
392
));
339
393
```
394
+
340
395
#### Result
396
+
341
397
``` json
342
398
[
343
399
{
@@ -356,16 +412,21 @@ Console.WriteLine(json.Get(key: "warehouse:1",
356
412
```
357
413
358
414
### Regex - Begins With <a name =" regex_begins " ></a >
415
+
359
416
Fetch all items within an array where a field begins with a given expression.
417
+
360
418
#### Command
419
+
361
420
``` c#
362
421
Console .WriteLine (json .Get (key : " warehouse:1" ,
363
422
path : " $.inventory[?(@.description =~ \" ^T\" )]" ,
364
423
indent : " \t " ,
365
424
newLine : " \n "
366
425
));
367
426
```
427
+
368
428
#### Result
429
+
369
430
``` json
370
431
[
371
432
{
0 commit comments