Skip to content

Commit 63deb6a

Browse files
Mryangedataroaring
authored andcommitted
[doc](function) json_valid input json value (#2850)
## Versions - [x] dev - [ ] 3.0 - [ ] 2.1 - [ ] 2.0 ## Languages - [x] Chinese - [x] English ## Docs Checklist - [ ] Checked by AI - [ ] Test Cases Built
1 parent 23ed6d8 commit 63deb6a

2 files changed

Lines changed: 94 additions & 50 deletions

File tree

  • 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

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

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,80 @@
88

99
## Description
1010

11-
The JSON_VALID function returns 0 or 1 to indicate whether the input is a valid JSON. If the input is NULL, it returns NULL.
11+
The JSON_VALID function is used to validate whether the input is valid JSON format. This function accepts String type or JSON type input and validates the JSON format.
1212

1313
## Syntax
1414

1515
```sql
16-
JSON_VALID( <str> )
17-
16+
JSON_VALID(<str>)
17+
JSON_VALID(<json>)
1818
```
1919

20-
Required Parameters
21-
| Parameter | Description |
22-
|------|------|
23-
| `<str>` | The input string in JSON format to be parsed. |
24-
25-
## Alias
26-
27-
- JSONB_VALID
28-
29-
## Examples
20+
## Parameters
3021

31-
1. Valid JSON string
22+
- `<str>`: String type, the JSON format string to be validated
23+
- `<json>`: JSON type, the JSON value to be validated
3224

33-
```sql
34-
SELECT json_valid('{"k1":"v31","k2":300}');
35-
+-------------------------------------+
36-
| json_valid('{"k1":"v31","k2":300}') |
37-
+-------------------------------------+
38-
| 1 |
39-
+-------------------------------------+
25+
## Return Values
4026

41-
```
27+
- `1`: When the input is valid JSON format
28+
- `0`: When the input is not valid JSON format
29+
- `NULL`: When the input parameter is NULL
4230

43-
2. Invalid JSON string
31+
## Notes
4432

45-
```sql
46-
SELECT json_valid('invalid json');
47-
+----------------------------+
48-
| json_valid('invalid json') |
49-
+----------------------------+
50-
| 0 |
51-
+----------------------------+
33+
The support for JSON type parameters is to avoid potential issues that might occur with implicit type conversion (JSON to String) when passing a JSON column. Typically, data stored in JSON type columns is valid JSON data, so calling JSON_VALID on a JSON type parameter usually returns 1.
5234

53-
```
35+
## Alias
5436

55-
3. NULL 参数
37+
- JSONB_VALID
5638

57-
```sql
58-
SELECT json_valid(NULL);
59-
+------------------+
60-
| json_valid(NULL) |
61-
+------------------+
62-
| NULL |
63-
+------------------+
39+
## Examples
6440

65-
```
41+
1. Validate a valid JSON string
42+
```sql
43+
SELECT json_valid('{"k1":"v31","k2":300}');
44+
```
45+
```text
46+
+-------------------------------------+
47+
| json_valid('{"k1":"v31","k2":300}') |
48+
+-------------------------------------+
49+
| 1 |
50+
+-------------------------------------+
51+
```
52+
53+
2. Validate an invalid JSON string
54+
```sql
55+
SELECT json_valid('invalid json');
56+
```
57+
```text
58+
+----------------------------+
59+
| json_valid('invalid json') |
60+
+----------------------------+
61+
| 0 |
62+
+----------------------------+
63+
```
64+
65+
3. Validate NULL parameter
66+
```sql
67+
SELECT json_valid(NULL);
68+
```
69+
```text
70+
+------------------+
71+
| json_valid(NULL) |
72+
+------------------+
73+
| NULL |
74+
+------------------+
75+
```
76+
77+
4. Validate JSON type parameter
78+
```sql
79+
SELECT json_valid(cast('{"k1":"v31","k2":300}' as json));
80+
```
81+
```text
82+
+----------------------------------------------------+
83+
| json_valid(cast('{"k1":"v31","k2":300}' as json)) |
84+
+----------------------------------------------------+
85+
| 1 |
86+
+----------------------------------------------------+
87+
```

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

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,33 @@
88

99
## 描述
1010

11-
JSON_VALID 函数用以判断字符串是否为有效的 JSON 字符串, 如果参数是 NULL 则返回 NULL
11+
JSON_VALID 函数用于验证输入是否为有效的 JSON 格式。该函数可以接受 String 类型或 JSON 类型的输入,对 JSON 格式进行验证
1212

1313
## 语法
1414

1515
```sql
16-
JSON_VALID( <str> )
16+
JSON_VALID(<str>)
17+
JSON_VALID(<json>)
1718
```
1819

19-
## 必选参数
20-
- `<str>` String 类型,需要判断的 JSON 格式字符串。
20+
## 参数说明
2121

22+
- `<str>`: String 类型,需要验证的 JSON 格式字符串
23+
- `<json>`: JSON 类型,需要验证的 JSON 值
2224

23-
## 举例
25+
## 返回值
2426

25-
1. 正常 JSON 字符串
27+
- `1`:当输入是有效的 JSON 格式时
28+
- `0`:当输入不是有效的 JSON 格式时
29+
- `NULL`:当输入参数为 NULL 时
30+
31+
## 注意事项
32+
33+
支持 JSON 类型参数的原因是避免当传入 JSON 列时发生隐式类型转换(JSON 转 String)可能导致的问题。通常情况下,JSON 类型的列中存储的都是有效的 JSON 数据,所以对 JSON 类型参数调用 JSON_VALID 通常返回 1。
34+
35+
## 示例
36+
37+
1. 验证有效的 JSON 字符串
2638
```sql
2739
SELECT json_valid('{"k1":"v31","k2":300}');
2840
```
@@ -32,10 +44,9 @@ JSON_VALID( <str> )
3244
+-------------------------------------+
3345
| 1 |
3446
+-------------------------------------+
35-
1 row in set (0.02 sec)
3647
```
3748

38-
2. 无效的 JSON 字符串
49+
2. 验证无效的 JSON 字符串
3950
```sql
4051
SELECT json_valid('invalid json');
4152
```
@@ -45,10 +56,9 @@ JSON_VALID( <str> )
4556
+----------------------------+
4657
| 0 |
4758
+----------------------------+
48-
4959
```
5060

51-
3. NULL 参数
61+
3. 验证 NULL 参数
5262
```sql
5363
SELECT json_valid(NULL);
5464
```
@@ -58,4 +68,16 @@ JSON_VALID( <str> )
5868
+------------------+
5969
| NULL |
6070
+------------------+
71+
```
72+
73+
4. 验证 JSON 类型参数
74+
```sql
75+
SELECT json_valid(cast('{"k1":"v31","k2":300}' as json));
76+
```
77+
```text
78+
+----------------------------------------------------+
79+
| json_valid(cast('{"k1":"v31","k2":300}' as json)) |
80+
+----------------------------------------------------+
81+
| 1 |
82+
+----------------------------------------------------+
6183
```

0 commit comments

Comments
 (0)