|
7 | 7 |
|
8 | 8 | ## Description
|
9 | 9 |
|
10 |
| -This function is used to check whether a JSON document contains a specified JSON element. If the specified element exists in the JSON document, it returns 1; otherwise, it returns 0. If the JSON document or the queried element is invalid, it returns `NULL`. |
| 10 | +Used to determine whether a JSON document contains a specified JSON element. If the specified element exists in the JSON document, it returns 1, otherwise it returns 0. If the JSON document or the queried element is invalid, it returns `NULL`. |
11 | 11 |
|
12 | 12 | ## Syntax
|
13 | 13 |
|
14 |
| -`JSON_CONTAINS(<json_str>, <candidate> [, <json_path>])` |
15 |
| - |
16 |
| -## Required Parameters |
17 |
| - |
18 |
| -| Parameter | Description | |
19 |
| -|-------------|-----------------------------------------------------| |
20 |
| -| `<json_str>` | The JSON string to be checked. | |
21 |
| -| `<candidate>` | The JSON element to check for inclusion. | |
22 |
| - |
23 |
| -## Optional Parameters |
24 |
| - |
25 |
| -| Parameter | Description | |
26 |
| -|-------------|-----------------------------------------------------| |
27 |
| -| `<json_path>` | An optional JSON path to specify the subdocument to check. If not provided, the root document is used by default. | |
28 |
| - |
29 |
| -## Return Value |
30 |
| -- If `<json_path>` exists in `json_doc`, it returns 1. |
31 |
| -- If `<json_path>` does not exist in `json_doc`, it returns 0. |
32 |
| -- If any parameter is invalid or the JSON document format is incorrect, it returns `NULL`. |
33 |
| - |
34 |
| -## Examples |
35 |
| - |
36 |
| -```sql |
37 |
| - |
38 |
| -SELECT JSON_CONTAINS('{"a": 1, "b": 2, "c": {"d": 4}}', '1', '$.a'); |
39 |
| - |
40 |
| -``` |
41 |
| - |
42 | 14 | ```sql
|
43 |
| -+------------------------------------------------------------------------------------------+ |
44 |
| -| json_contains(cast('{"a": 1, "b": 2, "c": {"d": 4}}' as JSON), cast('1' as JSON), '$.a') | |
45 |
| -+------------------------------------------------------------------------------------------+ |
46 |
| -| 1 | |
47 |
| -+------------------------------------------------------------------------------------------+ |
48 |
| - |
| 15 | +JSON_CONTAINS(<json_object>, <candidate>[, <json_path>]) |
49 | 16 | ```
|
50 | 17 |
|
| 18 | +## Parameters |
| 19 | +### Required Parameters |
| 20 | +- `<json_object>` JSON type, check whether `<candidate>` exists in it. |
| 21 | +- `<candidate>` JSON type, the candidate value to be determined. |
| 22 | +### Optional Parameters |
| 23 | +- `<json_path>` String type, the search starting path. If not provided, it starts from root by default. |
51 | 24 |
|
52 |
| -```sql |
53 |
| - |
54 |
| -SELECT json_contains('[1, 2, {"x": 3}]', '1'); |
55 |
| - |
56 |
| -``` |
57 |
| - |
58 |
| -```sql |
59 |
| -+-------------------------------------------------------------------------+ |
60 |
| -| json_contains(cast('[1, 2, {"x": 3}]' as JSON), cast('1' as JSON), '$') | |
61 |
| -+-------------------------------------------------------------------------+ |
62 |
| -| 1 | |
63 |
| -+-------------------------------------------------------------------------+ |
64 |
| - |
65 |
| -``` |
66 |
| - |
67 |
| - |
68 |
| - |
| 25 | +## Return Value |
| 26 | +- Null: If any of the three parameters is NULL, returns NULL |
| 27 | +- True: If `<json_object>` contains `<candidate>`, returns True. |
| 28 | +- False: If `<json_object>` does not contain `<candidate>`, returns False. |
| 29 | +- If `<json_object>` or `<candidate>` is not a JSON type, an error is reported. |
| 30 | + |
| 31 | +## Examples |
| 32 | +1. Example 1 |
| 33 | + ```sql |
| 34 | + SET @j = '{"a": 1, "b": 2, "c": {"d": 4}}'; |
| 35 | + SET @j2 = '1'; |
| 36 | + SELECT JSON_CONTAINS(@j, @j2, '$.a'); |
| 37 | + ``` |
| 38 | + ```text |
| 39 | + +-------------------------------+ |
| 40 | + | JSON_CONTAINS(@j, @j2, '$.a') | |
| 41 | + +-------------------------------+ |
| 42 | + | 1 | |
| 43 | + +-------------------------------+ |
| 44 | + ``` |
| 45 | + ```sql |
| 46 | + SELECT JSON_CONTAINS(@j, @j2, '$.b'); |
| 47 | + ``` |
| 48 | + ```text |
| 49 | + +-------------------------------+ |
| 50 | + | JSON_CONTAINS(@j, @j2, '$.b') | |
| 51 | + +-------------------------------+ |
| 52 | + | 0 | |
| 53 | + +-------------------------------+ |
| 54 | + ``` |
| 55 | + ```sql |
| 56 | + SELECT JSON_CONTAINS(@j, '{"a": 1}'); |
| 57 | + ``` |
| 58 | + ```text |
| 59 | + +-------------------------------+ |
| 60 | + | JSON_CONTAINS(@j, '{"a": 1}') | |
| 61 | + +-------------------------------+ |
| 62 | + | 1 | |
| 63 | + +-------------------------------+ |
| 64 | + ``` |
| 65 | +2. NULL parameters |
| 66 | + ```sql |
| 67 | + SELECT JSON_CONTAINS(NULL, '{"a": 1}'); |
| 68 | + ``` |
| 69 | + ```text |
| 70 | + +---------------------------------+ |
| 71 | + | JSON_CONTAINS(NULL, '{"a": 1}') | |
| 72 | + +---------------------------------+ |
| 73 | + | NULL | |
| 74 | + +---------------------------------+ |
| 75 | + ``` |
| 76 | + ```sql |
| 77 | + SELECT JSON_CONTAINS('{"a": 1}', NULL); |
| 78 | + ``` |
| 79 | + ```text |
| 80 | + +---------------------------------+ |
| 81 | + | JSON_CONTAINS('{"a": 1}', NULL) | |
| 82 | + +---------------------------------+ |
| 83 | + | NULL | |
| 84 | + +---------------------------------+ |
| 85 | + ``` |
| 86 | + ```sql |
| 87 | + SELECT JSON_CONTAINS('{"a": 1}', '{"a": 1}', NULL); |
| 88 | + ``` |
| 89 | + ```text |
| 90 | + +---------------------------------------------+ |
| 91 | + | JSON_CONTAINS('{"a": 1}', '{"a": 1}', NULL) | |
| 92 | + +---------------------------------------------+ |
| 93 | + | NULL | |
| 94 | + +---------------------------------------------+ |
| 95 | + ``` |
0 commit comments