You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To register the below JSON functions in your `SessionContext`.
17
16
17
+
# Examples
18
+
19
+
```sql
20
+
-- Create a table with a JSON column stored as a string
21
+
CREATETABLEtest_table (id INT, json_col VARCHAR) ASVALUES
22
+
(1, '{}'),
23
+
(2, '{ "a": 1 }'),
24
+
(3, '{ "a": 2 }'),
25
+
(4, '{ "a": 1, "b": 2 }'),
26
+
(5, '{ "a": 1, "b": 2, "c": 3 }');
27
+
28
+
-- Check if each document contains the key 'b'
29
+
SELECT id, json_contains(json_col, 'b') as json_contains FROM test_table;
30
+
-- Results in
31
+
-- +----+---------------+
32
+
-- | id | json_contains |
33
+
-- +----+---------------+
34
+
-- | 1 | false |
35
+
-- | 2 | false |
36
+
-- | 3 | false |
37
+
-- | 4 | true |
38
+
-- | 5 | true |
39
+
-- +----+---------------+
40
+
41
+
-- Get the value of the key 'a' from each document
42
+
SELECT id, json_col->'a'as json_col_a FROM test_table
43
+
44
+
-- +----+------------+
45
+
-- | id | json_col_a |
46
+
-- +----+------------+
47
+
-- | 1 | {null=} |
48
+
-- | 2 | {int=1} |
49
+
-- | 3 | {int=2} |
50
+
-- | 4 | {int=1} |
51
+
-- | 5 | {int=1} |
52
+
-- +----+------------+
53
+
```
54
+
55
+
18
56
## Done
19
57
20
58
*[x]`json_contains(json: str, *keys: str | int) -> bool` - true if a JSON string has a specific key (used for the `?` operator)
@@ -27,6 +65,11 @@ To register the below JSON functions in your `SessionContext`.
27
65
*[x]`json_as_text(json: str, *keys: str | int) -> str` - Get any value from a JSON string by its "path", represented as a string (used for the `->>` operator)
28
66
*[x]`json_length(json: str, *keys: str | int) -> int` - get the length of a JSON string or array
29
67
68
+
-[x]`->` operator - alias for `json_get`
69
+
-[x]`->>` operator - alias for `json_as_text`
70
+
-[x]`?` operator - alias for `json_contains`
71
+
72
+
### Notes
30
73
Cast expressions with `json_get` are rewritten to the appropriate method, e.g.
0 commit comments