Skip to content

Commit e617ade

Browse files
authored
update json_extract_xxx (#2629)
## Versions - [x] dev - [ ] 3.0 - [ ] 2.1 - [ ] 2.0 ## Languages - [x] Chinese - [x] English
1 parent 6c886a4 commit e617ade

22 files changed

+1131
-618
lines changed

docs/sql-manual/sql-functions/scalar-functions/json-functions/get-json-bigint.md

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,4 @@
77

88
## Description
99

10-
Parses and obtains the integer (BIGINT) content of the specified path within the json string.
11-
12-
## Syntax
13-
14-
```sql
15-
GET_JSON_BIGINT( <json_str>, <json_path>)
16-
```
17-
18-
## Required Parameters
19-
| parameters| described|
20-
|------|------|
21-
| `<json_str>`| The JSON string from which to extract data is needed. |
22-
| `<json_path>`| JSON path, specifying the location of the field. Paths can be denoted in dot notation. |
23-
24-
25-
## Return Value
26-
- Returns the BIGINT value of the field pointed to by the path.
27-
- Returns NULL if no corresponding field is found in the specified path, or the field value cannot be converted to type BIGINT.
28-
29-
## Usage Notes
30-
31-
Where `<json_str>` `must start with the $symbol and use. As a path splitter. If the path contains. , you can use double quotes to enclose it.
32-
Use [ ] to represent the array index, starting from 0.
33-
The content of path cannot contain ", [and].
34-
Returns NULL if the <json_str>format is incorrect, or the <json_path>format is incorrect, or a match cannot be found.
35-
In addition, it is recommended to use the jsonb type and the jsonb_extract_XXX function to achieve the same functionality.
36-
Special circumstances will be handled as follows:
37-
- Returns <json_path>NULL if the specified field does not exist in JSON
38-
- If <json_path>the actual type of the specified field in JSON is inconsistent with the type specified by json_extract_t, the specified type t will be returned if it can be losslessly converted to the specified type, and NULL will be returned if it cannot.
39-
40-
## Examples
41-
42-
1. Get the value with key as "k1"
43-
```sql
44-
SELECT get_json_bigint('{"k1":1, "k2":"2"}', "$.k1");
45-
```
46-
47-
```sql
48-
+-----------------------------------------------+
49-
| get_json_bigint('{"k1":1, "k2":"2"}', '$.k1') |
50-
+-----------------------------------------------+
51-
| 1 |
52-
+-----------------------------------------------+
53-
```
54-
2. Gets the second element in the array with key "my.key"
55-
```sql
56-
SELECT get_json_bigint('{"k1":"v1", "my.key":[1, 1678708107000, 3]}', '$. "my.key"[1]');
57-
```
58-
59-
```sql
60-
+---------------------------------------------------------------------------------+
61-
| get_json_bigint('{"k1":"v1", "my.key":[1, 1678708107000, 3]}', '$. "my.key"[1]') |
62-
+---------------------------------------------------------------------------------+
63-
| 1678708107000 |
64-
+---------------------------------------------------------------------------------+
65-
```
66-
3. Gets the first element in an array with secondary path k1.key -&gt; k2
67-
```sql
68-
SELECT get_json_bigint('{"k1.key":{"k2":[1678708107000, 2]}}', '$. "k1.key".k2[0]');
69-
```
70-
71-
```sql
72-
73-
+-----------------------------------------------------------------------------+
74-
| get_json_bigint('{"k1.key":{"k2":[1678708107000, 2]}}', '$."k1.key".k2[0]') |
75-
+-----------------------------------------------------------------------------+
76-
| 1678708107000 |
77-
+-----------------------------------------------------------------------------+
78-
79-
```
10+
Alias of function [`JSON_EXTRACT_BIGINT`](./json-extract-bigint.md).

docs/sql-manual/sql-functions/scalar-functions/json-functions/get-json-double.md

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,4 @@
77

88
## Description
99

10-
Function is used to extract the value of a field from a JSON document and convert it to the DOUBLE type. This function returns the value of the field on the specified path, or returns NULL if the value cannot be converted to type DOUBLE or the field pointed to by the path does not exist.
11-
12-
## Syntax
13-
14-
```sql
15-
GET_JSON_DOUBLE( <json_str>, <json_path>)
16-
```
17-
18-
## Required Parameters
19-
20-
| parameters| described|
21-
|------|------|
22-
| `<json_str>`| The JSON string from which to extract data is needed. |
23-
| `<json_path>`| JSON path, specifying the location of the field. Paths can be denoted in dot notation. |
24-
25-
## Return Value
26-
27-
- Returns the DOUBLE value of the field pointed to by the path.
28-
- Returns NULL if no corresponding field is found in the specified path, or the field value cannot be converted to type DOUBLE.
29-
30-
## Usage Notes
31-
32-
Parses and obtains the floating point content of the specified path within the json string.
33-
Where `<json_path>` `must start with the $symbol and use. As a path splitter. If the path contains. , you can use double quotes to enclose it.
34-
Use [ ] to represent the array index, starting from 0.
35-
The content of path cannot contain ", [and].
36-
Returns NULL if `<json_str>` is incorrectly formatted, or json_path is incorrectly formatted, or a match cannot be found.
37-
In addition, it is recommended to use the jsonb type and the jsonb_extract_XXX function to achieve the same functionality.
38-
Special circumstances will be handled as follows:
39-
- Returns NULL if the field specified by json_path does not exist in JSON
40-
- If the actual type of the field specified by json_path in JSON is inconsistent with the type specified by json_extract_t, the specified type t is returned if it can be losslessly converted to the specified type, and NULL is returned if it cannot.
41-
42-
## Examples
43-
44-
1. Get the value with key as "k1"
45-
46-
```sql
47-
SELECT get_json_double('{"k1":1.3, "k2":"2"}', "$.k1");
48-
```
49-
50-
```sql
51-
+-------------------------------------------------+
52-
| get_json_double('{"k1":1.3, "k2":"2"}', '$.k1') |
53-
+-------------------------------------------------+
54-
| 1.3 |
55-
+-------------------------------------------------+
56-
```
57-
2. Gets the second element in the array with key "my.key"
58-
59-
```sql
60-
SELECT get_json_double('{"k1":"v1", "my.key":[1.1, 2.2, 3.3]}', '$. "my.key"[1]');
61-
```
62-
63-
```sql
64-
+---------------------------------------------------------------------------+
65-
| get_json_double('{"k1":"v1", "my.key":[1.1, 2.2, 3.3]}', '$."my.key"[1]') |
66-
+---------------------------------------------------------------------------+
67-
| 2.2 |
68-
+---------------------------------------------------------------------------+
69-
```
70-
71-
3. Gets the first element in an array with secondary path k1.key -&gt; k2
72-
```sql
73-
SELECT get_json_double('{"k1.key":{"k2":[1.1, 2.2]}}', '$."k1.key".k2[0]');
74-
```
75-
```sql
76-
+---------------------------------------------------------------------+
77-
| get_json_double('{"k1.key":{"k2":[1.1, 2.2]}}', '$."k1.key".k2[0]') |
78-
+---------------------------------------------------------------------+
79-
| 1.1 |
80-
+---------------------------------------------------------------------+
81-
```
10+
Alias of function [`JSON_EXTRACT_DOUBLE`](./json-extract-double.md).

docs/sql-manual/sql-functions/scalar-functions/json-functions/get-json-int.md

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,73 +7,4 @@
77

88
## Description
99

10-
Function is used to extract the value of a field from a JSON document and convert it to type INT. This function returns the value of the field on the specified path, or returns NULL if the value cannot be converted to type INT or the field pointed to by the path does not exist.
11-
12-
## Syntax
13-
14-
```sql
15-
GET_JSON_INT( <json_str>, <json_path>)
16-
```
17-
18-
## Required Parameters
19-
20-
| parameters| described|
21-
|------|------|
22-
| `<json_str>`| The JSON string from which to extract data is needed. |
23-
| `<json_path>`| JSON path, specifying the location of the field. Paths can be denoted in dot notation. |
24-
25-
## Usage Notes
26-
27-
Parses and obtains the integer content of the specified path within the json string.
28-
Where `<json_path>` `must start with the $symbol and use. As a path splitter. If the path contains. , you can use double quotes to enclose it.
29-
Use [ ] to represent the array index, starting from 0.
30-
The content of path cannot contain ", [and].
31-
Returns NULL if the <json_str>format is incorrect, or the <json_path>format is incorrect, or a match cannot be found.
32-
In addition, it is recommended to use the jsonb type and the jsonb_extract_XXX function to achieve the same functionality.
33-
Special circumstances will be handled as follows:
34-
- Returns <json_path>NULL if the specified field does not exist in JSON
35-
- If <json_path>the actual type of the specified field in JSON is inconsistent with the type specified by json_extract_t, the specified type t will be returned if it can be losslessly converted to the specified type, and NULL will be returned if it cannot.
36-
37-
## Examples
38-
39-
1. Get the value with key as "k1"
40-
41-
```sql
42-
SELECT get_json_int('{"k1":1, "k2":"2"}', "$.k1");
43-
```
44-
45-
```sql
46-
47-
+--------------------------------------------+
48-
| get_json_int('{"k1":1, "k2":"2"}', '$.k1') |
49-
+--------------------------------------------+
50-
| 1 |
51-
+--------------------------------------------+
52-
```
53-
54-
2. Gets the second element in the array with key "my.key"
55-
56-
```sql
57-
SELECT get_json_int('{"k1":"v1", "my.key":[1, 2, 3]}', '$."my.key"[1]');
58-
```
59-
60-
```sql
61-
+------------------------------------------------------------------+
62-
| get_json_int('{"k1":"v1", "my.key":[1, 2, 3]}', '$."my.key"[1]') |
63-
+------------------------------------------------------------------+
64-
| 2 |
65-
+------------------------------------------------------------------+
66-
67-
3. Gets the first element in an array with secondary path k1.key -> k2
68-
69-
```sql
70-
SELECT get_json_int('{"k1.key":{"k2":[1, 2]}}', '$."k1.key".k2[0]');
71-
```
72-
73-
```sql
74-
+--------------------------------------------------------------+
75-
| get_json_int('{"k1.key":{"k2":[1, 2]}}', '$."k1.key".k2[0]') |
76-
+--------------------------------------------------------------+
77-
| 1 |
78-
+--------------------------------------------------------------+
79-
```
10+
Alias of function [`JSON_EXTRACT_INT`](./json-extract-int.md).

docs/sql-manual/sql-functions/scalar-functions/json-functions/get-json-string.md

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,4 @@
77

88
## Description
99

10-
This function is used to extract a field's value from a JSON document and convert it to `STRING` type. It returns the field value at the specified path. If the value cannot be converted to a string, or if the field at the specified path does not exist, it returns `NULL`.
11-
12-
## Syntax
13-
14-
```sql
15-
GET_JSON_STRING( <json_str>, <json_path>)
16-
```
17-
18-
## Required Parameters
19-
20-
| Parameter | Description |
21-
|-------------|-------------------------------------------------------|
22-
| `<json_str>` | The JSON string from which data needs to be extracted. |
23-
| `<json_path>` | JSON path that specifies the field's location. The path can use dot notation. |
24-
25-
## Return Value
26-
It returns the `STRING` value of the field at the specified path.
27-
If the specified path does not point to a valid field or the field value cannot be converted to a `STRING` type, it returns `NULL`.
28-
29-
## Usage Notes
30-
31-
Parses and retrieves the string content of the specified path in the JSON string.
32-
The `<json_path>` must start with the `$` symbol, using `.` as the path delimiter. If the path contains a `.`, it should be enclosed in double quotes.
33-
Use `[ ]` to indicate array indices, starting from 0.
34-
The path should not contain `", [`, and `]`.
35-
If the `<json_str>` format is incorrect, or if the `<json_path>` format is invalid, or if no matching field is found, `NULL` is returned.
36-
37-
Additionally, it is recommended to use the `jsonb` type and `jsonb_extract_XXX` functions to achieve the same functionality.
38-
39-
Special case handling as follows:
40-
- If the field specified by `<json_path>` does not exist in the JSON, return `NULL`.
41-
- If the actual type of the field specified by `<json_path>` differs from the type expected by `json_extract_t`, if it can be losslessly converted to the expected type, it will return the specified type. Otherwise, it will return `NULL`.
42-
43-
## Examples
44-
45-
1. Get the value for key "k1"
46-
47-
```sql
48-
49-
SELECT get_json_string('{"k1":"v1", "k2":"v2"}', "$.k1");
50-
```
51-
52-
```sql
53-
+---------------------------------------------------+
54-
| get_json_string('{"k1":"v1", "k2":"v2"}', '$.k1') |
55-
+---------------------------------------------------+
56-
| v1 |
57-
+---------------------------------------------------+
58-
```
59-
2. Get the second element of the array for key "my.key"
60-
61-
``` sql
62-
SELECT get_json_string('{"k1":"v1", "my.key":["e1", "e2", "e3"]}', '$."my.key"[1]');
63-
64-
```
65-
```sql
66-
+------------------------------------------------------------------------------+
67-
| get_json_string('{"k1":"v1", "my.key":["e1", "e2", "e3"]}', '$."my.key"[1]') |
68-
+------------------------------------------------------------------------------+
69-
| e2 |
70-
+------------------------------------------------------------------------------+
71-
72-
```
73-
3. Get the first element of the array in the secondary path k1.key -> k2
74-
75-
76-
```sql
77-
SELECT get_json_string('{"k1.key":{"k2":["v1", "v2"]}}', '$."k1.key".k2[0]');
78-
79-
```
80-
81-
```sql
82-
83-
+-----------------------------------------------------------------------+
84-
| get_json_string('{"k1.key":{"k2":["v1", "v2"]}}', '$."k1.key".k2[0]') |
85-
+-----------------------------------------------------------------------+
86-
| v1 |
87-
+-----------------------------------------------------------------------+
88-
89-
```
10+
Alias of function [`JSON_EXTRACT_STRING`](./json-extract-string.md).

docs/sql-manual/sql-functions/scalar-functions/json-functions/json-extract-bigint.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,82 @@
55
}
66
---
77

8+
## Description
9+
`JSON_EXTRACT_BIGINT` extracts the field specified by `<json_path>` from a JSON object and converts it to [`BIGINT`](../../../basic-element/sql-data-types/numeric/BIGINT.md) type.
10+
11+
## Syntax
12+
```sql
13+
JSON_EXTRACT_BIGINT(<json_object>, <json_path>)
14+
```
15+
16+
## Parameters
17+
- `<json_object>`: JSON type, the target parameter to extract from.
18+
- `<json_path>`: String type, the JSON path to extract the target element from the target JSON.
19+
20+
## Return Value
21+
`Nullable(BIGINT)` Returns the extracted BIGINT value, returns NULL in some cases
22+
23+
## Usage Notes
24+
1. If `<json_object>` or `<json_path>` is NULL, returns NULL.
25+
2. If the element specified by `<json_path>` does not exist, returns NULL.
26+
3. If the element specified by `<json_path>` cannot be converted to BIGINT, returns NULL.
27+
4. Its behavior is consistent with "cast + json_extract", which is equivalent to:
28+
```sql
29+
CAST(JSON_EXTRACT(<json_object>, <json_path>) as BIGINT)
30+
```
31+
32+
## Examples
33+
1. Normal parameters
34+
```sql
35+
SELECT json_extract_bigint('{"id": 122222222222223, "name": "doris"}', '$.id');
36+
```
37+
```text
38+
+-------------------------------------------------------------------------+
39+
| json_extract_bigint('{"id": 122222222222223, "name": "doris"}', '$.id') |
40+
+-------------------------------------------------------------------------+
41+
| 122222222222223 |
42+
+-------------------------------------------------------------------------+
43+
```
44+
2. Case where path does not exist
45+
```sql
46+
SELECT json_extract_bigint('{"id": 122222222222223, "name": "doris"}', '$.id2');
47+
```
48+
```text
49+
+--------------------------------------------------------------------------+
50+
| json_extract_bigint('{"id": 122222222222223, "name": "doris"}', '$.id2') |
51+
+--------------------------------------------------------------------------+
52+
| NULL |
53+
+--------------------------------------------------------------------------+
54+
```
55+
3. NULL parameters
56+
```sql
57+
SELECT json_extract_bigint('{"id": 122222222222223, "name": "doris"}', NULl);
58+
```
59+
```text
60+
+-----------------------------------------------------------------------+
61+
| json_extract_bigint('{"id": 122222222222223, "name": "doris"}', NULl) |
62+
+-----------------------------------------------------------------------+
63+
| NULL |
64+
+-----------------------------------------------------------------------+
65+
```
66+
```sql
67+
SELECT json_extract_bigint(NULL, '$.id2');
68+
```
69+
```text
70+
+------------------------------------+
71+
| json_extract_bigint(NULL, '$.id2') |
72+
+------------------------------------+
73+
| NULL |
74+
+------------------------------------+
75+
```
76+
4. Case where conversion to BIGINT is not possible
77+
```sql
78+
SELECT json_extract_bigint('{"id": 123, "name": "doris"}','$.name');
79+
```
80+
```text
81+
+--------------------------------------------------------------+
82+
| json_extract_bigint('{"id": 123, "name": "doris"}','$.name') |
83+
+--------------------------------------------------------------+
84+
| NULL |
85+
+--------------------------------------------------------------+
86+
```

0 commit comments

Comments
 (0)