Skip to content

Commit 6c886a4

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

File tree

4 files changed

+303
-219
lines changed
  • docs/sql-manual/sql-functions/scalar-functions/json-functions
  • i18n/zh-CN/docusaurus-plugin-content-docs
    • current/sql-manual/sql-functions/scalar-functions/json-functions
    • version-3.0/sql-manual/sql-functions/scalar-functions/json-functions
  • versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions

4 files changed

+303
-219
lines changed

docs/sql-manual/sql-functions/scalar-functions/json-functions/json-type.md

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,57 +26,83 @@ JSON_TYPE( <json>, <json_path> )
2626
```
2727

2828
## Alias
29+
- `JSONB_TYPE`
2930

30-
- JSONB_TYPE
31-
32-
## Required Parameters
33-
34-
35-
| Parameter | Description |
36-
|------|------|
37-
| `<json>` | The JSON string to check the type of. |
38-
| `<json_path>` | JSON path, which specifies the location of the field in JSON. The path is usually given in $. At the beginning, use. to represent the hierarchical structure. |
31+
## Parameters
3932

33+
- `<json>` The JSON string to check the type of.
34+
- `<json_path>` String type, which specifies the location of the field in JSON. The path is usually given in $. At the beginning, use. to represent the hierarchical structure.
4035

4136
## Return Value
42-
Returns the type of the JSON string. Possible values include:
43-
- "NULL": Indicates that the value in the JSON document is null.
44-
- "BOOLEAN": Indicates that the value in the JSON document is of boolean type (true or false).
45-
- "NUMBER": Indicates that the value in the JSON document is a number.
46-
- "STRING": Indicates that the value in the JSON document is a string.
47-
- "OBJECT": Indicates that the value in the JSON document is a JSON object.
48-
- "ARRAY": Indicates that the value in the JSON document is a JSON array.
4937

50-
## Usage Notes
38+
`Nullable<String>`: Returns the type of the corresponding field.
5139

52-
JSON_TYPE returns the type of the outermost value in the JSON document. If the JSON document contains multiple different types of values, it will return the type of the outermost value. For invalid JSON strings, JSON_TYPE returns NULL. Refer to [json tutorial](../../../basic-element/sql-data-types/semi-structured/JSON)
40+
## Usage Notes
41+
- If `<json_object>` or `<json_path>` is NULL, returns NULL.
42+
- If `<json_path>` is not a valid path, the function reports an error.
43+
- If the field specified by `<json_path>` does not exist, returns NULL.
5344

5445
## Examples
5546
1. JSON is of string type:
56-
57-
```sql
58-
SELECT JSON_TYPE('{"name": "John", "age": 30}', '$.name');
59-
```
60-
61-
```sql
62-
+-------------------------------------------------------------------+
63-
| jsonb_type(cast('{"name": "John", "age": 30}' as JSON), '$.name') |
64-
+-------------------------------------------------------------------+
65-
| string |
66-
+-------------------------------------------------------------------+
67-
```
47+
```sql
48+
SELECT JSON_TYPE('{"name": "John", "age": 30}', '$.name');
49+
```
50+
```text
51+
+-------------------------------------------------------------------+
52+
| jsonb_type(cast('{"name": "John", "age": 30}' as JSON), '$.name') |
53+
+-------------------------------------------------------------------+
54+
| string |
55+
+-------------------------------------------------------------------+
56+
```
6857

6958
2. JSON is of number type:
70-
71-
```sql
72-
SELECT JSON_TYPE('{"name": "John", "age": 30}', '$.age');
73-
```
74-
75-
```sql
76-
+------------------------------------------------------------------+
77-
| jsonb_type(cast('{"name": "John", "age": 30}' as JSON), '$.age') |
78-
+------------------------------------------------------------------+
79-
| int |
80-
+------------------------------------------------------------------+
81-
```
82-
59+
```sql
60+
SELECT JSON_TYPE('{"name": "John", "age": 30}', '$.age');
61+
```
62+
```text
63+
+------------------------------------------------------------------+
64+
| jsonb_type(cast('{"name": "John", "age": 30}' as JSON), '$.age') |
65+
+------------------------------------------------------------------+
66+
| int |
67+
+------------------------------------------------------------------+
68+
```
69+
3. NULL parameters
70+
```sql
71+
select json_type(NULL, '$.key1');
72+
```
73+
```text
74+
+---------------------------+
75+
| json_type(NULL, '$.key1') |
76+
+---------------------------+
77+
| NULL |
78+
+---------------------------+
79+
```
80+
4. NULL parameters 2
81+
```sql
82+
select json_type('{"key1": true}', NULL);
83+
```
84+
```text
85+
+-----------------------------------+
86+
| json_type('{"key1": true}', NULL) |
87+
+-----------------------------------+
88+
| NULL |
89+
+-----------------------------------+
90+
```
91+
5. Field specified by `json_path` parameter does not exist
92+
```sql
93+
select json_type('{"key1": true}', '$.key2');
94+
```
95+
```text
96+
+---------------------------------------+
97+
| json_type('{"key1": true}', '$.key2') |
98+
+---------------------------------------+
99+
| NULL |
100+
+---------------------------------------+
101+
```
102+
6. Invalid `json_path` parameter
103+
```sql
104+
select json_type('{"key1": true}', '$.');
105+
```
106+
```text
107+
ERROR 1105 (HY000): errCode = 2, detailMessage = [INVALID_ARGUMENT]Json path error: Invalid Json Path for value: $.
108+
```

i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-type.md

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,97 @@
66
---
77

88
## 描述
9-
10-
用来判断 json_path 指定的字段在 JSONB 数据中的类型,如果字段不存在返回 NULL,如果存在返回下面的类型之一
11-
12-
- object
13-
- array
14-
- null
15-
- bool
16-
- int
17-
- bigint
18-
- largeint
19-
- double
20-
- string
9+
用来判断 JSON 对象中 `<json_path>` 指定的字段的类型,如果字段不存在返回 NULL,如果存在返回下面的类型之一:
10+
* object
11+
* array
12+
* null
13+
* bool
14+
* int
15+
* bigint
16+
* largeint
17+
* double
18+
* string
2119

2220
## 语法
23-
2421
```sql
25-
STRING JSON_TYPE( <json> )
22+
JSON_TYPE(<json_object>, <json_path>)
2623
```
2724

2825
## 别名
26+
- `JSONB_TYPE`
2927

30-
- JSONB_TYPE
31-
32-
## 必选参数
33-
34-
| 参数 | 描述 |
35-
|------|------|
36-
| `<json>` | 需要检查类型的 JSON 字符串。 |
37-
28+
## 参数
29+
- `<json_object>`: [JSON 类型](../../../basic-element/sql-data-types/semi-structured/JSON.md) 的表达式。
30+
- `<json_path>`: String 类型,比如 `"$.key"`
3831

3932
## 返回值
40-
返回 JSON 字符串的类型,可能的值包括:
41-
- "NULL":表示 JSON 文档的值为 null。
42-
- "BOOLEAN":表示 JSON 文档的值为布尔类型(true 或 false)。
43-
- "NUMBER":表示 JSON 文档的值为数字类型。
44-
- "STRING":表示 JSON 文档的值为字符串类型。
45-
- "OBJECT":表示 JSON 文档的值为 JSON 对象。
46-
- "ARRAY":表示 JSON 文档的值为 JSON 数组。
47-
48-
## 注意事项
49-
50-
JSON_TYPE 返回的是 JSON 文档中最外层的值的类型。如果 JSON 文档包含多个不同类型的值,则返回最外层值的类型。
51-
对于无效的 JSON 字符串,JSON_TYPE 会返回 NULL。
52-
参考 [json tutorial](../../../basic-element/sql-data-types/semi-structured/JSON) 中的示例
33+
`Nullable<String>`: 返回对应字段的类型。
5334

35+
## 使用说明
36+
- 如果 `<json_object>` 或者 `<json_path>` 是 NULL,返回 NULL。
37+
- 如果 `<json_path>` 不是一个合法路径,函数报错。
38+
- 如果 `<json_path>` 指定的字段,返回 NULL。
5439

5540
## 示例
56-
1. JSON 为字符串类型
57-
58-
```sql
59-
SELECT JSON_TYPE('{"name": "John", "age": 30}', '$.name');
60-
```
61-
62-
```sql
63-
+-------------------------------------------------------------------+
64-
| jsonb_type(cast('{"name": "John", "age": 30}' as JSON), '$.name') |
65-
+-------------------------------------------------------------------+
66-
| string |
67-
+-------------------------------------------------------------------+
68-
```
69-
70-
2. JSON 为数字类型
71-
72-
```sql
73-
SELECT JSON_TYPE('{"name": "John", "age": 30}', '$.age');
74-
```
75-
76-
```sql
77-
+------------------------------------------------------------------+
78-
| jsonb_type(cast('{"name": "John", "age": 30}' as JSON), '$.age') |
79-
+------------------------------------------------------------------+
80-
| int |
81-
+------------------------------------------------------------------+
82-
```
41+
1. Double 类型
42+
```sql
43+
select json_type('{"key1": 1234.44}', '$.key1');
44+
```
45+
```
46+
+------------------------------------------+
47+
| json_type('{"key1": 1234.44}', '$.key1') |
48+
+------------------------------------------+
49+
| double |
50+
+------------------------------------------+
51+
```
52+
2. BOOLEAN 类型
53+
```sql
54+
select json_type('{"key1": true}', '$.key1');
55+
```
56+
```
57+
+---------------------------------------+
58+
| json_type('{"key1": true}', '$.key1') |
59+
+---------------------------------------+
60+
| bool |
61+
+---------------------------------------+
62+
```
63+
3. NULL 参数
64+
```sql
65+
select json_type(NULL, '$.key1');
66+
```
67+
```
68+
+---------------------------+
69+
| json_type(NULL, '$.key1') |
70+
+---------------------------+
71+
| NULL |
72+
+---------------------------+
73+
```
74+
4. NULL 参数 2
75+
```sql
76+
select json_type('{"key1": true}', NULL);
77+
```
78+
```
79+
+-----------------------------------+
80+
| json_type('{"key1": true}', NULL) |
81+
+-----------------------------------+
82+
| NULL |
83+
+-----------------------------------+
84+
```
85+
5. `json_path` 参数指定的字段不存在
86+
```sql
87+
select json_type('{"key1": true}', '$.key2');
88+
```
89+
```
90+
+---------------------------------------+
91+
| json_type('{"key1": true}', '$.key2') |
92+
+---------------------------------------+
93+
| NULL |
94+
+---------------------------------------+
95+
```
96+
6. 错误的 `json_path` 参数
97+
```sql
98+
select json_type('{"key1": true}', '$.');
99+
```
100+
```
101+
ERROR 1105 (HY000): errCode = 2, detailMessage = [INVALID_ARGUMENT]Json path error: Invalid Json Path for value: $.
102+
```

0 commit comments

Comments
 (0)