|
1 |
| -## Placeholder |
| 1 | +In this section of the JSONPath tutorial, filter and deep scan expressions will be presented using a set of simple documents. |
2 | 2 |
|
3 |
| -This is placeholder content. |
| 3 | +```redis Load documents |
| 4 | +JSON.SET lit1 $ 5 |
| 5 | +JSON.SET lit2 $ '"abc"' |
| 6 | +JSON.SET lit3 $ true |
| 7 | +JSON.SET lit4 $ null |
| 8 | +JSON.SET obj1 $ '{"a":1, "b":2}' |
| 9 | +JSON.SET obj2 $ '{"a":[1,2,3,"a","b","c",false,true,["a",1],{"a":1},{"b":null}], "b":5}' |
| 10 | +JSON.SET obj3 $ '{"a1":"xx", "b1":"xx", "c1":"yy", "d1":".."}' |
| 11 | +JSON.SET arr1 $ [1,2,3] |
| 12 | +JSON.SET arr2 $ [[1,2,3],4,5,6] |
| 13 | +JSON.SET arrmap $ '[{"a":1}, {"b":2}]' |
| 14 | +``` |
| 15 | + |
| 16 | +## Deep scan (recursive decent) expressions |
| 17 | + |
| 18 | +Deep scan (recursive descent) is available anywhere a name is required. |
| 19 | +All values and sub-arrays of any sub-objects are processed, and then each of their elements are processed recursively. |
| 20 | + |
| 21 | +Here are a few examples: |
| 22 | + |
| 23 | +```redis Deep scan of a nested array document |
| 24 | +JSON.GET arr2 $..* |
| 25 | +``` |
| 26 | + |
| 27 | +```redis Deep scan of a nested object document |
| 28 | +JSON.GET obj2 $..* |
| 29 | +``` |
| 30 | + |
| 31 | +```redis Deep scan of an object's sub-element b |
| 32 | +JSON.GET obj2 '$..["b"]' |
| 33 | +``` |
| 34 | + |
| 35 | +## Filter expressions |
| 36 | + |
| 37 | +Filter expressions provide the ability to retrieve items using a combination of relational and logical operators. |
| 38 | +Filter expressions use the following syntax: |
| 39 | + |
| 40 | +`[? filterexpr ]` |
| 41 | + |
| 42 | +where `filterexpr` must evaluate to a boolean value, `true` or `false`. |
| 43 | + |
| 44 | +The `@` operator, used only in filter expressions, represents the current node: an array, object, or scalar value. |
| 45 | + |
| 46 | +Supported relational operators: `<`, `<=`, `==`, `>=`, `>`, `!=`, and `=~`. The last operator is used for regular expression matching, and there are two cases: |
| 47 | + |
| 48 | +1. operands that match a regular expression |
| 49 | +1. operands that match a regular expression at a given path |
| 50 | + |
| 51 | +Supported logical operators: `||` and `&&`. |
| 52 | + |
| 53 | +### Relational filter expression examples |
| 54 | + |
| 55 | +```redis Simple relational filter expressions |
| 56 | +JSON.GET arr1 '$[?@>1]' |
| 57 | +JSON.GET obj2 '$.a[?@=="a"]' |
| 58 | +``` |
| 59 | + |
| 60 | +```redis More filter expressions (identical results) |
| 61 | +JSON.GET obj2 '$.a[?@a==1]' |
| 62 | +JSON.GET obj2 '$.a[?@.a==1]' |
| 63 | +JSON.GET obj2 '$.a[?@*==1]' |
| 64 | +JSON.GET obj2 '$.a[?@.*==1]' |
| 65 | +``` |
| 66 | + |
| 67 | +Some interesting relational use cases: |
| 68 | + |
| 69 | +```redis Interesting filter expressions |
| 70 | +JSON.GET obj2 '$.a[?@==@]' // returns all elements |
| 71 | +JSON.GET obj2 '$.a[?@[0]==@[0]]' // returns all elements that are non-empty arrays |
| 72 | +JSON.GET obj2 '$.a[?@*==@*]' // returns all elements that are non-empty objects |
| 73 | +``` |
| 74 | + |
| 75 | +#### Relational use cases using `<`, `<=`, `==`, `!=` `>=`, `>` |
| 76 | + |
| 77 | +All evaluations of operand1 where operand1 and operand2 evaluate to scalar values (number, string, `true`, `false`, or `null`) of the same type and the comparison holds. When there's a mismatch in type, an empty array is returned. |
| 78 | + |
| 79 | +The comparison is arithmetic for numbers and lexicographic for strings. For Booleans, `false` < `true`. |
| 80 | + |
| 81 | +Neither arrays nor objects can be used as operands. |
| 82 | + |
| 83 | +The `!=` operator is effective only when there's a mismatch in either type or value. |
| 84 | + |
| 85 | +```redis Relational filter expressions using <, <=, >=, and > |
| 86 | +JSON.GET obj2 '$.a[?@>1]' // [2,3] |
| 87 | +JSON.GET obj2 '$.a[?1<@]' // [2,3] |
| 88 | +JSON.GET obj2 '$.a[?@>"a"]' // ["b","c"] |
| 89 | +JSON.GET obj2 '$.a[?"a"<@]' // ["b","c"] |
| 90 | +JSON.GET obj2 '$.a[?@>false]' // [true] |
| 91 | +JSON.GET obj2 '$.a[?false<@]' // [true] |
| 92 | +JSON.GET obj2 '$.a[?1<=1]' // [1,2,3,"a","b","c",false,true,["a",1],{"a":1},{"b":null}], all values |
| 93 | +JSON.GET obj2 '$.a[?@<=@]' // [1,2,3,"a","b","c",false,true], all scalar values |
| 94 | +JSON.GET obj2 '$.a[?@<=true]' // [false,true], all Boolean values |
| 95 | +JSON.GET obj2 '$.a[?(@<0 || @>=0)]' // [1,2,3], all numeric values |
| 96 | +``` |
| 97 | + |
| 98 | +```redis Relational filter expressions using == |
| 99 | +JSON.GET obj2 '$.a[?@==1]' // [1] |
| 100 | +JSON.GET obj2 '$.a[?1==@]' // [1] |
| 101 | +JSON.GET obj2 '$.a[?@=="a"]' // ["a"] |
| 102 | +JSON.GET obj2 '$.a[?@==false]' // [false] |
| 103 | +JSON.GET obj2 '$.a[?@==@]' // [1,2,3,"a","b","c",false,true,["a",1],{"a":1},{"b":null}], all values |
| 104 | +JSON.GET obj2 '$.a[?1==1]' // [1,2,3,"a","b","c",false,true,["a",1],{"a":1},{"b":null}], all values |
| 105 | +``` |
| 106 | + |
| 107 | +```redis Relational filter expressions using != |
| 108 | +JSON.GET obj2 '$.a[?(1!=@)]' // [2,3,"a","b","c",false,true,["a",1],{"a":1},{"b":null}] |
| 109 | +JSON.GET obj2 '$.a[?(@!="a")]' // [1,2,3,"b","c",false,true,["a",1],{"a":1},{"b":null}] |
| 110 | +``` |
| 111 | + |
| 112 | +#### Relational use cases using regular expression with the `=~` operator |
| 113 | + |
| 114 | +**Note**: |
| 115 | +> Redis Stack uses [Rust regular expressions syntax](https://docs.rs/regex/latest/regex/#syntax). Invalid regular expressions are not evaluated. |
| 116 | +
|
| 117 | +There are two cases: |
| 118 | + |
| 119 | +1. operands that match a specific regular expression |
| 120 | + |
| 121 | +```redis Regex expressions using direct match |
| 122 | +JSON.GET obj2 '$.a[?@ =~ "(?i)"]' // ["a","b","c"] |
| 123 | +``` |
| 124 | + |
| 125 | +1. operands that match a regular expression at a given path |
| 126 | + |
| 127 | +```redis Regex expressions using match at a specific path |
| 128 | +JSON.GET obj3 '$[?@ =~ $.a1]' // ["xx","xx"], matches of the regex "xx" |
| 129 | +JSON.GET obj3 '$[?@ =~ $.d1]' // ["xx","xx","yy",".."], matches of the regex ".." (any 2 chars) |
| 130 | +``` |
| 131 | + |
| 132 | +### Logical filter expression examples using `&&` and `||` |
| 133 | + |
| 134 | +```redis Logical filter expression examples |
| 135 | +JSON.GET arr1 '$.*[?(@>1 && @<3)]' // [2], all operands must evaluate to true |
| 136 | +JSON.GET arr1 '$.*[?(@<2 || @>2)]' // [1,3], at least one operand must evaluate to true |
| 137 | +``` |
| 138 | + |
| 139 | +### Filter expression examples involving literals |
| 140 | + |
| 141 | +**Note**: |
| 142 | +> Arrays and objects cannot be used as literals. |
| 143 | +
|
| 144 | +```redis Filter expression examples involving literals |
| 145 | +JSON.GET obj2 '$.a[?@==1]' // "[1]", number literal |
| 146 | +JSON.GET obj2 '$.a[?@=="a"]' // "[\"a\"]", string literal |
| 147 | +JSON.GET obj2 '$.a[?(@==true)]' // "[true]", Boolean literal |
| 148 | +JSON.GET obj2 '$.a[?(@==false)]' // "[false]", Boolean literal |
| 149 | +JSON.GET obj2 '$.a[?(@.b==null)]' // "[{\"b\":null}]", nil literal |
| 150 | +JSON.GET obj2 '$.a[*].*[?(@==null)]' // "[null]", nil literal |
| 151 | +``` |
0 commit comments