This is an automated email from the ASF dual-hosted git repository. kassiez pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris-website.git
The following commit(s) were added to refs/heads/master by this push: new d79b4479dab [doc] Fix some json function zh_doc (#1827) d79b4479dab is described below commit d79b4479dab554dcd806f45e85efbb5470758159 Author: mklzl <36362816+mk...@users.noreply.github.com> AuthorDate: Tue Jan 21 15:42:51 2025 +0800 [doc] Fix some json function zh_doc (#1827) - [x] zh_json_quote - [x] zh_json_unquote - [x] zh_json_extract - [x] zh_json_insert - [x] zh_json_object - [x] zh_json_replace ## Versions - [x] dev - [x] 3.0 - [x] 2.1 - [ ] 2.0 ## Languages - [x] Chinese - [ ] English ## Docs Checklist - [ ] Checked by AI - [ ] Test Cases Built --- .../json-functions/json-extract.md | 177 +++++++++++++++------ .../scalar-functions/json-functions/json-insert.md | 54 +++++-- .../scalar-functions/json-functions/json-object.md | 59 +++++-- .../scalar-functions/json-functions/json-quote.md | 43 +++-- .../json-functions/json-replace.md | 55 +++++-- .../json-functions/json-unquote.md | 82 ++++++++-- .../json-functions/json-extract.md | 177 +++++++++++++++------ .../scalar-functions/json-functions/json-insert.md | 54 +++++-- .../scalar-functions/json-functions/json-object.md | 60 +++++-- .../scalar-functions/json-functions/json-quote.md | 43 +++-- .../json-functions/json-replace.md | 55 +++++-- .../json-functions/json-unquote.md | 83 ++++++++-- .../json-functions/json-extract.md | 176 ++++++++++++++------ .../scalar-functions/json-functions/json-insert.md | 54 +++++-- .../scalar-functions/json-functions/json-object.md | 59 +++++-- .../json-functions/json-replace.md | 55 +++++-- .../json-functions/json-unquote.md | 82 ++++++++-- 17 files changed, 1009 insertions(+), 359 deletions(-) diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md index 97147c795e3..340c14e02a5 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md @@ -24,84 +24,163 @@ specific language governing permissions and limitations under the License. --> -## json_extract - ## 描述 -## 语法 +JSON_EXTRACT 是一系列函数,从 JSON 类型的数据中提取 json_path 指定的字段,根据要提取的字段类型不同提供不同的系列函数。 +* JSON_EXTRACT 对 VARCHAR 类型的 json string 返回 VARCHAR 类型 +* JSON_EXTRACT_ISNULL 返回是否为 json null 的 BOOLEAN 类型 +* JSON_EXTRACT_BOOL 返回 BOOLEAN 类型 +* JSON_EXTRACT_INT 返回 INT 类型 +* JSON_EXTRACT_BIGINT 返回 BIGINT 类型 +* JSON_EXTRACT_LARGEINT 返回 LARGEINT 类型 +* JSON_EXTRACT_DOUBLE 返回 DOUBLE 类型 +* JSON_EXTRACT_STRING 返回 STRING 类型 + +## 别名 +* JSONB_EXTRACT 同 `JSON_EXTRACT` +* JSONB_EXTRACT_ISNULL 同 `JSON_EXTRACT_ISNULL` +* JSONB_EXTRACT_BOOL 同 `JSON_EXTRACT_BOOL` +* JSONB_EXTRACT_INT 同 `JSON_EXTRACT_INT` +* JSONB_EXTRACT_BIGINT 同 `JSON_EXTRACT_BIGINT` +* JSONB_EXTRACT_LARGEINT 同 `JSON_EXTRACT_LARGEINT` +* JSONB_EXTRACT_DOUBLE 同 `JSON_EXTRACT_DOUBLE` +* JSONB_EXTRACT_STRING 同 `JSON_EXTRACT_STRING` +## 语法 ```sql -VARCHAR json_extract(VARCHAR json_str, VARCHAR path[, VARCHAR path] ...) -JSON jsonb_extract(JSON j, VARCHAR json_path) -BOOLEAN json_extract_isnull(JSON j, VARCHAR json_path) -BOOLEAN json_extract_bool(JSON j, VARCHAR json_path) -INT json_extract_int(JSON j, VARCHAR json_path) -BIGINT json_extract_bigint(JSON j, VARCHAR json_path) -LARGEINT json_extract_largeint(JSON j, VARCHAR json_path) -DOUBLE json_extract_double(JSON j, VARCHAR json_path) -STRING json_extract_string(JSON j, VARCHAR json_path) +JSON_EXTRACT (<json_str>, <path>[, path] ...) ``` +```sql +JSON_EXTRACT_ISNULL (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_BOOL (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_INT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_BIGINT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_LARGEINT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_DOUBLE (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_STRING (<json_str>, <path>) +``` +别名函数除函数名称之外,语法和用法与上述函数保持一致。 +## 参数 +| 参数 | 描述 | +|--------------|-----------------------------| +| `<json_str>` | 要提取的 JSON 类型的参数或者字段 | +| `<path>` | 要从目标 JSON 中提取目标元素的 JSON 路径。 | +`json_path` 的语法如下 +* '$' 代表 json root +* '.k1' 代表 json object 中 key 为'k1'的元素 + - 如果 key 列值包含 ".", json_path 中需要用双引号,例如 `SELECT json_extract('{"k1.a":"abc","k2":300}', '$."k1.a"');` +* '[i]' 代表 json array 中下标为 i 的元素 + - 获取 json_array 的最后一个元素可以用'$[last]',倒数第二个元素可以用'$[last-1]',以此类推。 -json_extract 是一系列函数,从 JSON 类型的数据中提取 json_path 指定的字段,根据要提取的字段类型不同提供不同的系列函数。 -- json_extract 对 VARCHAR 类型的 json string 返回 VARCHAR 类型 -- jsonb_extract 返回 JSON 类型 -- json_extract_isnull 返回是否为 json null 的 BOOLEAN 类型 -- json_extract_bool 返回 BOOLEAN 类型 -- json_extract_int 返回 INT 类型 -- json_extract_bigint 返回 BIGINT 类型 -- json_extract_largeint 返回 LARGEINT 类型 -- json_extract_double 返回 DOUBLE 类型 -- json_extract_STRING 返回 STRING 类型 - -json path 的语法如下 -- '$' 代表 json root -- '.k1' 代表 json object 中 key 为'k1'的元素 - - 如果 key 列值包含 ".", json_path 中需要用双引号,例如 SELECT json_extract('{"k1.a":"abc","k2":300}', '$."k1.a"'); -- '[i]' 代表 json array 中下标为 i 的元素 - - 获取 json_array 的最后一个元素可以用'$[last]',倒数第二个元素可以用'$[last-1]',以此类推 - -特殊情况处理如下: -- 如果 json_path 指定的字段在 JSON 中不存在,返回 NULL -- 如果 json_path 指定的字段在 JSON 中的实际类型和 json_extract_t 指定的类型不一致,如果能无损转换成指定类型返回指定类型 t,如果不能则返回 NULL +## 返回值 +根据要提取的字段类型不同,返回目标 JSON 中 指定 JSON_PATH 的数据类型。特殊情况处理如下: +* 如果 json_path 指定的字段在 JSON 中不存在,返回 NULL +* 如果 json_path 指定的字段在 JSON 中的实际类型和 json_extract_t 指定的类型不一致,如果能无损转换成指定类型返回指定类型 t,如果不能则返回 NULL -## 举例 -参考 [json tutorial](../../sql-reference/Data-Types/JSON.md) 中的示例 +## 示例 +```sql +SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); ``` -mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); + +```text +------------------------------------------------------+ | json_extract('{"id": 123, "name": "doris"}', '$.id') | +------------------------------------------------------+ | 123 | +------------------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('[1, 2, 3]', '$.[1]'); +``` +```sql +SELECT json_extract('[1, 2, 3]', '$.[1]'); +``` +```text +------------------------------------+ | json_extract('[1, 2, 3]', '$.[1]') | +------------------------------------+ | 2 | +------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]'); +``` +```sql +SELECT json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]'); +``` +```text +-------------------------------------------------------------------------------------------------------------------+ | json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]') | +-------------------------------------------------------------------------------------------------------------------+ | ["v1",6.6,[1,2],2] | +-------------------------------------------------------------------------------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name'); +``` +```sql +SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name'); +``` +```text +-----------------------------------------------------------------+ | json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name') | +-----------------------------------------------------------------+ | [null,"doris"] | +-----------------------------------------------------------------+ -1 row in set (0.01 sec) ``` - -### keywords -JSONB, JSON, json_extract, json_extract_isnull, json_extract_bool, json_extract_int, json_extract_bigint, json_extract_largeint, json_extract_double, json_extract_string +```sql +SELECT JSON_EXTRACT_ISNULL('{"id": 123, "name": "doris"}', '$.id'); +``` +```text ++----------------------------------------------------------------------------+ +| jsonb_extract_isnull(cast('{"id": 123, "name": "doris"}' as JSON), '$.id') | ++----------------------------------------------------------------------------+ +| 0 | ++----------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_BOOL('{"id": 123, "name": "NULL"}', '$.id'); +``` +```text ++-------------------------------------------------------------------------+ +| jsonb_extract_bool(cast('{"id": 123, "name": "NULL"}' as JSON), '$.id') | ++-------------------------------------------------------------------------+ +| NULL | ++-------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_INT('{"id": 123, "name": "NULL"}', '$.id'); +``` +```text ++------------------------------------------------------------------------+ +| jsonb_extract_int(cast('{"id": 123, "name": "NULL"}' as JSON), '$.id') | ++------------------------------------------------------------------------+ +| 123 | ++------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_INT('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++---------------------------------------------------------------------------+ +| jsonb_extract_int(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++---------------------------------------------------------------------------+ +| NULL | ++---------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_STRING('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++------------------------------------------------------------------------------+ +| jsonb_extract_string(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++------------------------------------------------------------------------------+ +| doris | ++------------------------------------------------------------------------------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md index c3fca96a7de..bd59551f03c 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md @@ -24,18 +24,23 @@ specific language governing permissions and limitations under the License. --> -## json_insert - - ## 描述 -## 语法 +`JSON_INSERT` 函数用于在 JSON 中插入数据并返回结果。 -`VARCHAR json_insert(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## 语法 +```sql +JSON_INSERT (<json_str>, <path>, <val>[, <path>, <val>, ...]) +``` +## 参数 +| 参数 | 描述 | +|-------------|--------------------------------------------------------------------------------------------------------------------------------| +| `<json_str>` | 要插入的 JSON 对象。可以是任意类型元素的 JSON 对象,包括`NULL`,如果没有指定元素,则返回一个空数组。如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误 | +| `<path>` | 要插入的 JSON 路径。如果是 `NULL` ,则返回 NULL | +| `<val>` | 要插入 JSON 的值。如果是 `NULL` ,则会在对应的位置插入 `NULL` 的 value 值。 | -`json_insert` 函数在 JSON 中插入数据并返回结果。如果 `json_str` 或 `path` 为 NULL,则返回 NULL。否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 -路径值对按从左到右的顺序进行评估。 +需要注意的是,路径值对按从左到右的顺序进行评估。 如果 JSON 中不存在该路径,则路径值对会添加该值到 JSON 中,如果路径标识某个类型的值,则: @@ -44,30 +49,47 @@ under the License. 否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 -## 举例 +## 返回值 +返回一个 JSON 值。 +## 示例 +```sql +select json_insert(null, null, null); ``` -MySQL> select json_insert(null, null, null); +```text +---------------------------------+ | json_insert(NULL, NULL, 'NULL') | +---------------------------------+ | NULL | +---------------------------------+ - -MySQL> select json_insert('{"k": 1}', "$.k", 2); +``` +```sql +select json_insert('{"k": 1}', "$.k", 2); +``` +```text +---------------------------------------+ | json_insert('{\"k\": 1}', '$.k', '2') | +---------------------------------------+ | {"k":1} | +---------------------------------------+ - -MySQL> select json_insert('{"k": 1}', "$.j", 2); +``` +```sql +select json_insert('{"k": 1}', "$.j", 2); +``` +```text +---------------------------------------+ | json_insert('{\"k\": 1}', '$.j', '2') | +---------------------------------------+ | {"k":1,"j":2} | +---------------------------------------+ ``` - -### keywords -JSON, json_insert +```sql +select json_insert('{"k": 1}', "$.j", null); +``` +```text ++-----------------------------------------------+ +| json_insert('{"k": 1}', '$.j', 'NULL', '660') | ++-----------------------------------------------+ +| {"k":1,"j":null} | ++-----------------------------------------------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md index 7d8fbd2253f..82d9dfa36e3 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md @@ -24,47 +24,76 @@ specific language governing permissions and limitations under the License. --> -## json_object ## 描述 + +生成一个或者多个包含指定 Key-Value 对的 json object, 当 Key 值为 NULL 或者传入参数为奇数个时,返回异常错误。 + ## 语法 -`VARCHAR json_object(VARCHAR,...)` +```sql +JSON_OBJECT (<key>, <value>[,<key>, <value>, ...]) +``` + +## 参数 + +| 参数 | 描述 | +|---------|------------------------------------------| +| `<key>` | 指定生成的 json object 的 Key-Value 中的 Key 值 | +| `<value>` | 指定生成的 json object 的 Key-Value 中的 Value 值 | +## 返回值 -生成一个包含指定Key-Value对的json object, 当Key值为NULL或者传入参数为奇数个时,返回异常错误 +返回一个 json object。特殊情况如下: +* 如果没有传入参数,返回一个空的 json object。 +* 如果传入的参数个数为奇数个,返回异常错误。 +* 如果传入的 Key 为 NULL,返回异常错误。 +* 如果传入的 Value 为 NULL,返回的 json object 中该 Key-Value 对的 Value 值为 NULL。 -## 举例 +## 示例 +```sql +select json_object(); ``` -MySQL> select json_object(); +```text +---------------+ | json_object() | +---------------+ | {} | +---------------+ - -MySQL> select json_object('time',curtime()); +``` +```sql +select json_object('time',curtime()); +``` +```text +--------------------------------+ | json_object('time', curtime()) | +--------------------------------+ | {"time": "10:49:18"} | +--------------------------------+ - - -MySQL> SELECT json_object('id', 87, 'name', 'carrot'); +``` +```sql +SELECT json_object('id', 87, 'name', 'carrot'); +``` +```text +-----------------------------------------+ | json_object('id', 87, 'name', 'carrot') | +-----------------------------------------+ | {"id": 87, "name": "carrot"} | +-----------------------------------------+ - - -MySQL> select json_object('username',null); +``` +```sql +select json_object('username',null); +``` +```text +---------------------------------+ | json_object('username', 'NULL') | +---------------------------------+ | {"username": NULL} | +---------------------------------+ ``` -### keywords -json,object,json_object +```sql +select json_object(null,null); +``` +```text +ERROR 1105 (HY000): errCode = 2, detailMessage = json_object key can't be NULL: json_object(NULL) +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md index ee7ebff5992..f1088230ccd 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md @@ -24,47 +24,66 @@ specific language governing permissions and limitations under the License. --> -## json_quote ## 描述 +将 json_value 用双引号(")括起来,跳过其中包含的特殊转义字符。 + ## 语法 +```sql +JSON_QUOTE (<a>) +``` -`VARCHAR json_quote(VARCHAR)` +## 参数 +| 参数 | 描述 | +|------|---------------------| +| `<a>` | 要括起来的 json_value 的值 | -将json_value用双引号(")括起来,跳过其中包含的特殊转义字符 +## 返回值 +返回一个 json_value。特殊情况如下: +* 如果传入的参数为 NULL,返回 NULL。 ## 举例 +```sql +SELECT json_quote('null'), json_quote('"null"'); ``` -MySQL> SELECT json_quote('null'), json_quote('"null"'); + +```text +--------------------+----------------------+ | json_quote('null') | json_quote('"null"') | +--------------------+----------------------+ | "null" | "\"null\"" | +--------------------+----------------------+ +``` - -MySQL> SELECT json_quote('[1, 2, 3]'); +```sql +SELECT json_quote('[1, 2, 3]'); +``` +```text +-------------------------+ | json_quote('[1, 2, 3]') | +-------------------------+ | "[1, 2, 3]" | +-------------------------+ - - -MySQL> SELECT json_quote(null); +``` +```sql +SELECT json_quote(null); +``` +```text +------------------+ | json_quote(null) | +------------------+ | NULL | +------------------+ +``` -MySQL> select json_quote("\n\b\r\t"); +```sql +select json_quote("\n\b\r\t"); +``` +```text +------------------------+ | json_quote('\n\b\r\t') | +------------------------+ | "\n\b\r\t" | +------------------------+ ``` -### keywords -json,quote,json_quote diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md index 944ccd09e1d..016437a8021 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md @@ -24,45 +24,70 @@ specific language governing permissions and limitations under the License. --> -## json_replace - ## 描述 +`JSON_REPLACE` 函数用于在 JSON 中更新数据并返回结果。 + ## 语法 +```sql +JSON_REPLACE (<json_str>, <path>, <val>[, <jsonPath>, <val>, ...]) +``` +## 参数 +| 参数 | 描述 | +|--------------|---------------------------------------------------------------------------------------------| +| `<json_str>` | 要替换的 JSON 数据。可以是任意类型元素的 JSON 对象,包括`NULL`,如果没有指定元素,则返回一个空数组。如果 `json_str` 不是有效的 JSON,则会返回错误 | +| `<path>` | 要替换的 JSON 路径。 | +| `<val>` | 要替换 JSON_PATH Key 对应 value 的值。如果是 `NULL` ,则会在对应的位置插入 `NULL` 的 value 值。 | + -`VARCHAR json_replace(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## 返回值 +如果 `json_str` 和 `path` 都为 NULL,则返回 NULL。 -`json_replace` 函数在 JSON 中更新数据并返回结果。如果 `json_str` 或 `path` 为 NULL,则返回 NULL。否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 +否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 路径值对按从左到右的顺序进行评估。 -如果 JSON 中已存在某个路径,则路径值对会将现有 JSON 值覆盖为新值。 -否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 +如果 JSON 中已存在某个路径,则路径值对会将现有 JSON 值覆盖为新值。否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 -## 举例 +## 示例 +```sql +select json_replace(null, null, null); ``` -MySQL> select json_replace(null, null, null); +```text +----------------------------------+ | json_replace(NULL, NULL, 'NULL') | +----------------------------------+ | NULL | +----------------------------------+ - -MySQL> select json_replace('{"k": 1}', "$.k", 2); +``` +```sql +select json_replace('{"k": 1}', "$.k", 2); +``` +```text +----------------------------------------+ | json_replace('{\"k\": 1}', '$.k', '2') | +----------------------------------------+ | {"k":2} | +----------------------------------------+ - -MySQL> select json_replace('{"k": 1}', "$.j", 2); +``` +```sql +select json_replace('{"k": 1}', "$.j", 2); +``` +```text +----------------------------------------+ | json_replace('{\"k\": 1}', '$.j', '2') | +----------------------------------------+ | {"k":1} | +----------------------------------------+ ``` - -### keywords -JSON, json_replace +```sql +select json_replace(null, null, 's'); +``` +```text ++--------------------------------------+ +| json_replace(NULL, NULL, 's', '006') | ++--------------------------------------+ +| NULL | ++--------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md index c7eeb0f44fe..8120d320d65 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md @@ -24,13 +24,8 @@ specific language governing permissions and limitations under the License. --> -## json_unquote ## 描述 -## 语法 - -`VARCHAR json_unquote(VARCHAR)` - -这个函数将去掉JSON值中的引号,并将结果作为utf8mb4字符串返回。如果参数为NULL,则返回NULL。 +这个函数将去掉 JSON 值中的引号,并将结果作为 utf8mb4 字符串返回。如果参数为 NULL,则返回 NULL。 在字符串中显示的如下转义序列将被识别,对于所有其他转义序列,反斜杠将被忽略。 @@ -46,38 +41,91 @@ under the License. | \uxxxx | Unicode 值 XXXX 的 UTF-8 字节 | +## 语法 +```sql +JSON_UNQUOTE (<a>) +``` -## 举例 +## 参数 +| 参数 | 描述 | +|------|--------------------------------------------------------| +| `<a>` | 要去除引号的元素。 | + +## 返回值 +返回一个 utf8mb4 字符串。特殊情况如下: +* 如果传入的参数为 NULL,返回 NULL。 +* 如果传入的参数不是一个带有双引号的值,则会返回值本身。 +* 如果传入的参数不是一个字符串,则会被自动转换为字符串后,返回值本身。 +## 举例 +```sql +SELECT json_unquote('"doris"'); ``` -mysql> SELECT json_unquote('"doris"'); + +```text +-------------------------+ | json_unquote('"doris"') | +-------------------------+ | doris | +-------------------------+ - -mysql> SELECT json_unquote('[1, 2, 3]'); +``` +```sql +SELECT json_unquote('[1, 2, 3]'); +``` +```text +---------------------------+ | json_unquote('[1, 2, 3]') | +---------------------------+ | [1, 2, 3] | +---------------------------+ - - -mysql> SELECT json_unquote(null); +``` +```sql +SELECT json_unquote(null); +``` +```text +--------------------+ | json_unquote(NULL) | +--------------------+ | NULL | +--------------------+ - -mysql> SELECT json_unquote('"\\ttest"'); +``` +```sql +SELECT json_unquote('"\\ttest"'); +``` +```text +--------------------------+ | json_unquote('"\ttest"') | +--------------------------+ | test | +--------------------------+ ``` -### keywords -json,unquote,json_unquote +```sql +select json_unquote('"doris'); +``` +```text ++------------------------+ +| json_unquote('"doris') | ++------------------------+ +| "doris | ++------------------------+ +``` +```sql +select json_unquote('doris'); +``` +```text ++-----------------------+ +| json_unquote('doris') | ++-----------------------+ +| doris | ++-----------------------+ +``` +```sql +select json_unquote(1); +``` +```text ++-----------------------------------------+ +| json_unquote(cast(1 as VARCHAR(65533))) | ++-----------------------------------------+ +| 1 | ++-----------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md index 97147c795e3..2fafafecfb6 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md @@ -24,84 +24,163 @@ specific language governing permissions and limitations under the License. --> -## json_extract - ## 描述 -## 语法 +JSON_EXTRACT 是一系列函数,从 JSON 类型的数据中提取 json_path 指定的字段,根据要提取的字段类型不同提供不同的系列函数。 +* JSON_EXTRACT 对 VARCHAR 类型的 json string 返回 VARCHAR 类型 +* JSON_EXTRACT_ISNULL 返回是否为 json null 的 BOOLEAN 类型 +* JSON_EXTRACT_BOOL 返回 BOOLEAN 类型 +* JSON_EXTRACT_INT 返回 INT 类型 +* JSON_EXTRACT_BIGINT 返回 BIGINT 类型 +* JSON_EXTRACT_LARGEINT 返回 LARGEINT 类型 +* JSON_EXTRACT_DOUBLE 返回 DOUBLE 类型 +* JSON_EXTRACT_STRING 返回 STRING 类型 + +## 别名 +* JSONB_EXTRACT 同 `JSON_EXTRACT` +* JSONB_EXTRACT_ISNULL 同 `JSON_EXTRACT_ISNULL` +* JSONB_EXTRACT_BOOL 同 `JSON_EXTRACT_BOOL` +* JSONB_EXTRACT_INT 同 `JSON_EXTRACT_INT` +* JSONB_EXTRACT_BIGINT 同 `JSON_EXTRACT_BIGINT` +* JSONB_EXTRACT_LARGEINT 同 `JSON_EXTRACT_LARGEINT` +* JSONB_EXTRACT_DOUBLE 同 `JSON_EXTRACT_DOUBLE` +* JSONB_EXTRACT_STRING 同 `JSON_EXTRACT_STRING` +## 语法 ```sql -VARCHAR json_extract(VARCHAR json_str, VARCHAR path[, VARCHAR path] ...) -JSON jsonb_extract(JSON j, VARCHAR json_path) -BOOLEAN json_extract_isnull(JSON j, VARCHAR json_path) -BOOLEAN json_extract_bool(JSON j, VARCHAR json_path) -INT json_extract_int(JSON j, VARCHAR json_path) -BIGINT json_extract_bigint(JSON j, VARCHAR json_path) -LARGEINT json_extract_largeint(JSON j, VARCHAR json_path) -DOUBLE json_extract_double(JSON j, VARCHAR json_path) -STRING json_extract_string(JSON j, VARCHAR json_path) +JSON_EXTRACT (<json_str>, <path>[, path] ...) ``` +```sql +JSON_EXTRACT_ISNULL (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_BOOL (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_INT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_BIGINT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_LARGEINT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_DOUBLE (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_STRING (<json_str>, <path>) +``` +别名函数除函数名称之外,语法和用法与上述函数保持一致。 +## 参数 +| 参数 | 描述 | +|--------------|-----------------------------| +| `<json_str>` | 要提取的 JSON 类型的参数或者字段 | +| `<path>` | 要从目标 JSON 中提取目标元素的 JSON 路径。 | +`json_path` 的语法如下 +* '$' 代表 json root +* '.k1' 代表 json object 中 key 为'k1'的元素 + - 如果 key 列值包含 ".", json_path 中需要用双引号,例如 `SELECT json_extract('{"k1.a":"abc","k2":300}', '$."k1.a"');` +* '[i]' 代表 json array 中下标为 i 的元素 + - 获取 json_array 的最后一个元素可以用'$[last]',倒数第二个元素可以用'$[last-1]',以此类推。 -json_extract 是一系列函数,从 JSON 类型的数据中提取 json_path 指定的字段,根据要提取的字段类型不同提供不同的系列函数。 -- json_extract 对 VARCHAR 类型的 json string 返回 VARCHAR 类型 -- jsonb_extract 返回 JSON 类型 -- json_extract_isnull 返回是否为 json null 的 BOOLEAN 类型 -- json_extract_bool 返回 BOOLEAN 类型 -- json_extract_int 返回 INT 类型 -- json_extract_bigint 返回 BIGINT 类型 -- json_extract_largeint 返回 LARGEINT 类型 -- json_extract_double 返回 DOUBLE 类型 -- json_extract_STRING 返回 STRING 类型 - -json path 的语法如下 -- '$' 代表 json root -- '.k1' 代表 json object 中 key 为'k1'的元素 - - 如果 key 列值包含 ".", json_path 中需要用双引号,例如 SELECT json_extract('{"k1.a":"abc","k2":300}', '$."k1.a"'); -- '[i]' 代表 json array 中下标为 i 的元素 - - 获取 json_array 的最后一个元素可以用'$[last]',倒数第二个元素可以用'$[last-1]',以此类推 - -特殊情况处理如下: -- 如果 json_path 指定的字段在 JSON 中不存在,返回 NULL -- 如果 json_path 指定的字段在 JSON 中的实际类型和 json_extract_t 指定的类型不一致,如果能无损转换成指定类型返回指定类型 t,如果不能则返回 NULL +## 返回值 +根据要提取的字段类型不同,返回目标 JSON 中 指定 JSON_PATH 的数据类型。特殊情况处理如下: +* 如果 json_path 指定的字段在 JSON 中不存在,返回 NULL +* 如果 json_path 指定的字段在 JSON 中的实际类型和 json_extract_t 指定的类型不一致,如果能无损转换成指定类型返回指定类型 t,如果不能则返回 NULL -## 举例 -参考 [json tutorial](../../sql-reference/Data-Types/JSON.md) 中的示例 +## 示例 +```sql +SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); ``` -mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); + +```text +------------------------------------------------------+ | json_extract('{"id": 123, "name": "doris"}', '$.id') | +------------------------------------------------------+ | 123 | +------------------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('[1, 2, 3]', '$.[1]'); +``` +```sql +SELECT json_extract('[1, 2, 3]', '$.[1]'); +``` +```text +------------------------------------+ | json_extract('[1, 2, 3]', '$.[1]') | +------------------------------------+ | 2 | +------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]'); +``` +```sql +SELECT json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]'); +``` +```text +-------------------------------------------------------------------------------------------------------------------+ | json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]') | +-------------------------------------------------------------------------------------------------------------------+ | ["v1",6.6,[1,2],2] | +-------------------------------------------------------------------------------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name'); +``` +```sql +SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name'); +``` +```text +-----------------------------------------------------------------+ | json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name') | +-----------------------------------------------------------------+ | [null,"doris"] | +-----------------------------------------------------------------+ -1 row in set (0.01 sec) ``` - -### keywords -JSONB, JSON, json_extract, json_extract_isnull, json_extract_bool, json_extract_int, json_extract_bigint, json_extract_largeint, json_extract_double, json_extract_string +```sql +SELECT JSON_EXTRACT_ISNULL('{"id": 123, "name": "doris"}', '$.id'); +``` +```text ++----------------------------------------------------------------------------+ +| jsonb_extract_isnull(cast('{"id": 123, "name": "doris"}' as JSON), '$.id') | ++----------------------------------------------------------------------------+ +| 0 | ++----------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_BOOL('{"id": 123, "name": "NULL"}', '$.id'); +``` +```text ++-------------------------------------------------------------------------+ +| jsonb_extract_bool(cast('{"id": 123, "name": "NULL"}' as JSON), '$.id') | ++-------------------------------------------------------------------------+ +| NULL | ++-------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_INT('{"id": 123, "name": "NULL"}', '$.id'); +``` +```text ++------------------------------------------------------------------------+ +| jsonb_extract_int(cast('{"id": 123, "name": "NULL"}' as JSON), '$.id') | ++------------------------------------------------------------------------+ +| 123 | ++------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_INT('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++---------------------------------------------------------------------------+ +| jsonb_extract_int(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++---------------------------------------------------------------------------+ +| NULL | ++---------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_STRING('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++------------------------------------------------------------------------------+ +| jsonb_extract_string(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++------------------------------------------------------------------------------+ +| doris | ++------------------------------------------------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md index c3fca96a7de..bd59551f03c 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md @@ -24,18 +24,23 @@ specific language governing permissions and limitations under the License. --> -## json_insert - - ## 描述 -## 语法 +`JSON_INSERT` 函数用于在 JSON 中插入数据并返回结果。 -`VARCHAR json_insert(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## 语法 +```sql +JSON_INSERT (<json_str>, <path>, <val>[, <path>, <val>, ...]) +``` +## 参数 +| 参数 | 描述 | +|-------------|--------------------------------------------------------------------------------------------------------------------------------| +| `<json_str>` | 要插入的 JSON 对象。可以是任意类型元素的 JSON 对象,包括`NULL`,如果没有指定元素,则返回一个空数组。如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误 | +| `<path>` | 要插入的 JSON 路径。如果是 `NULL` ,则返回 NULL | +| `<val>` | 要插入 JSON 的值。如果是 `NULL` ,则会在对应的位置插入 `NULL` 的 value 值。 | -`json_insert` 函数在 JSON 中插入数据并返回结果。如果 `json_str` 或 `path` 为 NULL,则返回 NULL。否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 -路径值对按从左到右的顺序进行评估。 +需要注意的是,路径值对按从左到右的顺序进行评估。 如果 JSON 中不存在该路径,则路径值对会添加该值到 JSON 中,如果路径标识某个类型的值,则: @@ -44,30 +49,47 @@ under the License. 否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 -## 举例 +## 返回值 +返回一个 JSON 值。 +## 示例 +```sql +select json_insert(null, null, null); ``` -MySQL> select json_insert(null, null, null); +```text +---------------------------------+ | json_insert(NULL, NULL, 'NULL') | +---------------------------------+ | NULL | +---------------------------------+ - -MySQL> select json_insert('{"k": 1}', "$.k", 2); +``` +```sql +select json_insert('{"k": 1}', "$.k", 2); +``` +```text +---------------------------------------+ | json_insert('{\"k\": 1}', '$.k', '2') | +---------------------------------------+ | {"k":1} | +---------------------------------------+ - -MySQL> select json_insert('{"k": 1}', "$.j", 2); +``` +```sql +select json_insert('{"k": 1}', "$.j", 2); +``` +```text +---------------------------------------+ | json_insert('{\"k\": 1}', '$.j', '2') | +---------------------------------------+ | {"k":1,"j":2} | +---------------------------------------+ ``` - -### keywords -JSON, json_insert +```sql +select json_insert('{"k": 1}', "$.j", null); +``` +```text ++-----------------------------------------------+ +| json_insert('{"k": 1}', '$.j', 'NULL', '660') | ++-----------------------------------------------+ +| {"k":1,"j":null} | ++-----------------------------------------------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md index 7d8fbd2253f..bfe2ba6aa4e 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md @@ -24,47 +24,77 @@ specific language governing permissions and limitations under the License. --> -## json_object + ## 描述 + +生成一个或者多个包含指定 Key-Value 对的 json object, 当 Key 值为 NULL 或者传入参数为奇数个时,返回异常错误。 + ## 语法 -`VARCHAR json_object(VARCHAR,...)` +```sql +JSON_OBJECT (<key>, <value>[,<key>, <value>, ...]) +``` + +## 参数 +| 参数 | 描述 | +|---------|------------------------------------------| +| `<key>` | 指定生成的 json object 的 Key-Value 中的 Key 值 | +| `<value>` | 指定生成的 json object 的 Key-Value 中的 Value 值 | -生成一个包含指定Key-Value对的json object, 当Key值为NULL或者传入参数为奇数个时,返回异常错误 +## 返回值 -## 举例 +返回一个 json object。特殊情况如下: +* 如果没有传入参数,返回一个空的 json object。 +* 如果传入的参数个数为奇数个,返回异常错误。 +* 如果传入的 Key 为 NULL,返回异常错误。 +* 如果传入的 Value 为 NULL,返回的 json object 中该 Key-Value 对的 Value 值为 NULL。 +## 示例 + +```sql +select json_object(); ``` -MySQL> select json_object(); +```text +---------------+ | json_object() | +---------------+ | {} | +---------------+ - -MySQL> select json_object('time',curtime()); +``` +```sql +select json_object('time',curtime()); +``` +```text +--------------------------------+ | json_object('time', curtime()) | +--------------------------------+ | {"time": "10:49:18"} | +--------------------------------+ - - -MySQL> SELECT json_object('id', 87, 'name', 'carrot'); +``` +```sql +SELECT json_object('id', 87, 'name', 'carrot'); +``` +```text +-----------------------------------------+ | json_object('id', 87, 'name', 'carrot') | +-----------------------------------------+ | {"id": 87, "name": "carrot"} | +-----------------------------------------+ - - -MySQL> select json_object('username',null); +``` +```sql +select json_object('username',null); +``` +```text +---------------------------------+ | json_object('username', 'NULL') | +---------------------------------+ | {"username": NULL} | +---------------------------------+ ``` -### keywords -json,object,json_object +```sql +select json_object(null,null); +``` +```text +ERROR 1105 (HY000): errCode = 2, detailMessage = json_object key can't be NULL: json_object(NULL) +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md index ee7ebff5992..f1088230ccd 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md @@ -24,47 +24,66 @@ specific language governing permissions and limitations under the License. --> -## json_quote ## 描述 +将 json_value 用双引号(")括起来,跳过其中包含的特殊转义字符。 + ## 语法 +```sql +JSON_QUOTE (<a>) +``` -`VARCHAR json_quote(VARCHAR)` +## 参数 +| 参数 | 描述 | +|------|---------------------| +| `<a>` | 要括起来的 json_value 的值 | -将json_value用双引号(")括起来,跳过其中包含的特殊转义字符 +## 返回值 +返回一个 json_value。特殊情况如下: +* 如果传入的参数为 NULL,返回 NULL。 ## 举例 +```sql +SELECT json_quote('null'), json_quote('"null"'); ``` -MySQL> SELECT json_quote('null'), json_quote('"null"'); + +```text +--------------------+----------------------+ | json_quote('null') | json_quote('"null"') | +--------------------+----------------------+ | "null" | "\"null\"" | +--------------------+----------------------+ +``` - -MySQL> SELECT json_quote('[1, 2, 3]'); +```sql +SELECT json_quote('[1, 2, 3]'); +``` +```text +-------------------------+ | json_quote('[1, 2, 3]') | +-------------------------+ | "[1, 2, 3]" | +-------------------------+ - - -MySQL> SELECT json_quote(null); +``` +```sql +SELECT json_quote(null); +``` +```text +------------------+ | json_quote(null) | +------------------+ | NULL | +------------------+ +``` -MySQL> select json_quote("\n\b\r\t"); +```sql +select json_quote("\n\b\r\t"); +``` +```text +------------------------+ | json_quote('\n\b\r\t') | +------------------------+ | "\n\b\r\t" | +------------------------+ ``` -### keywords -json,quote,json_quote diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md index 944ccd09e1d..016437a8021 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md @@ -24,45 +24,70 @@ specific language governing permissions and limitations under the License. --> -## json_replace - ## 描述 +`JSON_REPLACE` 函数用于在 JSON 中更新数据并返回结果。 + ## 语法 +```sql +JSON_REPLACE (<json_str>, <path>, <val>[, <jsonPath>, <val>, ...]) +``` +## 参数 +| 参数 | 描述 | +|--------------|---------------------------------------------------------------------------------------------| +| `<json_str>` | 要替换的 JSON 数据。可以是任意类型元素的 JSON 对象,包括`NULL`,如果没有指定元素,则返回一个空数组。如果 `json_str` 不是有效的 JSON,则会返回错误 | +| `<path>` | 要替换的 JSON 路径。 | +| `<val>` | 要替换 JSON_PATH Key 对应 value 的值。如果是 `NULL` ,则会在对应的位置插入 `NULL` 的 value 值。 | + -`VARCHAR json_replace(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## 返回值 +如果 `json_str` 和 `path` 都为 NULL,则返回 NULL。 -`json_replace` 函数在 JSON 中更新数据并返回结果。如果 `json_str` 或 `path` 为 NULL,则返回 NULL。否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 +否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 路径值对按从左到右的顺序进行评估。 -如果 JSON 中已存在某个路径,则路径值对会将现有 JSON 值覆盖为新值。 -否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 +如果 JSON 中已存在某个路径,则路径值对会将现有 JSON 值覆盖为新值。否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 -## 举例 +## 示例 +```sql +select json_replace(null, null, null); ``` -MySQL> select json_replace(null, null, null); +```text +----------------------------------+ | json_replace(NULL, NULL, 'NULL') | +----------------------------------+ | NULL | +----------------------------------+ - -MySQL> select json_replace('{"k": 1}', "$.k", 2); +``` +```sql +select json_replace('{"k": 1}', "$.k", 2); +``` +```text +----------------------------------------+ | json_replace('{\"k\": 1}', '$.k', '2') | +----------------------------------------+ | {"k":2} | +----------------------------------------+ - -MySQL> select json_replace('{"k": 1}', "$.j", 2); +``` +```sql +select json_replace('{"k": 1}', "$.j", 2); +``` +```text +----------------------------------------+ | json_replace('{\"k\": 1}', '$.j', '2') | +----------------------------------------+ | {"k":1} | +----------------------------------------+ ``` - -### keywords -JSON, json_replace +```sql +select json_replace(null, null, 's'); +``` +```text ++--------------------------------------+ +| json_replace(NULL, NULL, 's', '006') | ++--------------------------------------+ +| NULL | ++--------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md index c7eeb0f44fe..1b00ad9eabe 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md @@ -24,13 +24,9 @@ specific language governing permissions and limitations under the License. --> -## json_unquote -## 描述 -## 语法 -`VARCHAR json_unquote(VARCHAR)` - -这个函数将去掉JSON值中的引号,并将结果作为utf8mb4字符串返回。如果参数为NULL,则返回NULL。 +## 描述 +这个函数将去掉 JSON 值中的引号,并将结果作为 utf8mb4 字符串返回。如果参数为 NULL,则返回 NULL。 在字符串中显示的如下转义序列将被识别,对于所有其他转义序列,反斜杠将被忽略。 @@ -46,38 +42,91 @@ under the License. | \uxxxx | Unicode 值 XXXX 的 UTF-8 字节 | +## 语法 +```sql +JSON_UNQUOTE (<a>) +``` -## 举例 +## 参数 +| 参数 | 描述 | +|------|--------------------------------------------------------| +| `<a>` | 要去除引号的元素。 | +## 返回值 +返回一个 utf8mb4 字符串。特殊情况如下: +* 如果传入的参数为 NULL,返回 NULL。 +* 如果传入的参数不是一个带有双引号的值,则会返回值本身。 +* 如果传入的参数不是一个字符串,则会被自动转换为字符串后,返回值本身。 + +## 举例 +```sql +SELECT json_unquote('"doris"'); ``` -mysql> SELECT json_unquote('"doris"'); + +```text +-------------------------+ | json_unquote('"doris"') | +-------------------------+ | doris | +-------------------------+ - -mysql> SELECT json_unquote('[1, 2, 3]'); +``` +```sql +SELECT json_unquote('[1, 2, 3]'); +``` +```text +---------------------------+ | json_unquote('[1, 2, 3]') | +---------------------------+ | [1, 2, 3] | +---------------------------+ - - -mysql> SELECT json_unquote(null); +``` +```sql +SELECT json_unquote(null); +``` +```text +--------------------+ | json_unquote(NULL) | +--------------------+ | NULL | +--------------------+ - -mysql> SELECT json_unquote('"\\ttest"'); +``` +```sql +SELECT json_unquote('"\\ttest"'); +``` +```text +--------------------------+ | json_unquote('"\ttest"') | +--------------------------+ | test | +--------------------------+ ``` -### keywords -json,unquote,json_unquote +```sql +select json_unquote('"doris'); +``` +```text ++------------------------+ +| json_unquote('"doris') | ++------------------------+ +| "doris | ++------------------------+ +``` +```sql +select json_unquote('doris'); +``` +```text ++-----------------------+ +| json_unquote('doris') | ++-----------------------+ +| doris | ++-----------------------+ +``` +```sql +select json_unquote(1); +``` +```text ++-----------------------------------------+ +| json_unquote(cast(1 as VARCHAR(65533))) | ++-----------------------------------------+ +| 1 | ++-----------------------------------------+ +``` \ No newline at end of file diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md index 97147c795e3..f0478c9b905 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md @@ -24,84 +24,164 @@ specific language governing permissions and limitations under the License. --> -## json_extract - ## 描述 -## 语法 +JSON_EXTRACT 是一系列函数,从 JSON 类型的数据中提取 json_path 指定的字段,根据要提取的字段类型不同提供不同的系列函数。 +* JSON_EXTRACT 对 VARCHAR 类型的 json string 返回 VARCHAR 类型 +* JSON_EXTRACT_ISNULL 返回是否为 json null 的 BOOLEAN 类型 +* JSON_EXTRACT_BOOL 返回 BOOLEAN 类型 +* JSON_EXTRACT_INT 返回 INT 类型 +* JSON_EXTRACT_BIGINT 返回 BIGINT 类型 +* JSON_EXTRACT_LARGEINT 返回 LARGEINT 类型 +* JSON_EXTRACT_DOUBLE 返回 DOUBLE 类型 +* JSON_EXTRACT_STRING 返回 STRING 类型 + +## 别名 +* JSONB_EXTRACT 同 `JSON_EXTRACT` +* JSONB_EXTRACT_ISNULL 同 `JSON_EXTRACT_ISNULL` +* JSONB_EXTRACT_BOOL 同 `JSON_EXTRACT_BOOL` +* JSONB_EXTRACT_INT 同 `JSON_EXTRACT_INT` +* JSONB_EXTRACT_BIGINT 同 `JSON_EXTRACT_BIGINT` +* JSONB_EXTRACT_LARGEINT 同 `JSON_EXTRACT_LARGEINT` +* JSONB_EXTRACT_DOUBLE 同 `JSON_EXTRACT_DOUBLE` +* JSONB_EXTRACT_STRING 同 `JSON_EXTRACT_STRING` +## 语法 ```sql -VARCHAR json_extract(VARCHAR json_str, VARCHAR path[, VARCHAR path] ...) -JSON jsonb_extract(JSON j, VARCHAR json_path) -BOOLEAN json_extract_isnull(JSON j, VARCHAR json_path) -BOOLEAN json_extract_bool(JSON j, VARCHAR json_path) -INT json_extract_int(JSON j, VARCHAR json_path) -BIGINT json_extract_bigint(JSON j, VARCHAR json_path) -LARGEINT json_extract_largeint(JSON j, VARCHAR json_path) -DOUBLE json_extract_double(JSON j, VARCHAR json_path) -STRING json_extract_string(JSON j, VARCHAR json_path) +JSON_EXTRACT (<json_str>, <path>[, path] ...) ``` +```sql +JSON_EXTRACT_ISNULL (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_BOOL (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_INT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_BIGINT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_LARGEINT (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_DOUBLE (<json_str>, <path>) +``` +```sql +JSON_EXTRACT_STRING (<json_str>, <path>) +``` +别名函数除函数名称之外,语法和用法与上述函数保持一致。 +## 参数 +| 参数 | 描述 | +|--------------|-----------------------------| +| `<json_str>` | 要提取的 JSON 类型的参数或者字段 | +| `<path>` | 要从目标 JSON 中提取目标元素的 JSON 路径。 | +`json_path` 的语法如下 +* '$' 代表 json root +* '.k1' 代表 json object 中 key 为'k1'的元素 + - 如果 key 列值包含 ".", json_path 中需要用双引号,例如 `SELECT json_extract('{"k1.a":"abc","k2":300}', '$."k1.a"');` +* '[i]' 代表 json array 中下标为 i 的元素 + - 获取 json_array 的最后一个元素可以用'$[last]',倒数第二个元素可以用'$[last-1]',以此类推。 -json_extract 是一系列函数,从 JSON 类型的数据中提取 json_path 指定的字段,根据要提取的字段类型不同提供不同的系列函数。 -- json_extract 对 VARCHAR 类型的 json string 返回 VARCHAR 类型 -- jsonb_extract 返回 JSON 类型 -- json_extract_isnull 返回是否为 json null 的 BOOLEAN 类型 -- json_extract_bool 返回 BOOLEAN 类型 -- json_extract_int 返回 INT 类型 -- json_extract_bigint 返回 BIGINT 类型 -- json_extract_largeint 返回 LARGEINT 类型 -- json_extract_double 返回 DOUBLE 类型 -- json_extract_STRING 返回 STRING 类型 - -json path 的语法如下 -- '$' 代表 json root -- '.k1' 代表 json object 中 key 为'k1'的元素 - - 如果 key 列值包含 ".", json_path 中需要用双引号,例如 SELECT json_extract('{"k1.a":"abc","k2":300}', '$."k1.a"'); -- '[i]' 代表 json array 中下标为 i 的元素 - - 获取 json_array 的最后一个元素可以用'$[last]',倒数第二个元素可以用'$[last-1]',以此类推 - -特殊情况处理如下: -- 如果 json_path 指定的字段在 JSON 中不存在,返回 NULL -- 如果 json_path 指定的字段在 JSON 中的实际类型和 json_extract_t 指定的类型不一致,如果能无损转换成指定类型返回指定类型 t,如果不能则返回 NULL +## 返回值 +根据要提取的字段类型不同,返回目标 JSON 中 指定 JSON_PATH 的数据类型。特殊情况处理如下: +* 如果 json_path 指定的字段在 JSON 中不存在,返回 NULL +* 如果 json_path 指定的字段在 JSON 中的实际类型和 json_extract_t 指定的类型不一致,如果能无损转换成指定类型返回指定类型 t,如果不能则返回 NULL -## 举例 -参考 [json tutorial](../../sql-reference/Data-Types/JSON.md) 中的示例 +## 示例 +```sql +SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); ``` -mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); + +```text +------------------------------------------------------+ | json_extract('{"id": 123, "name": "doris"}', '$.id') | +------------------------------------------------------+ | 123 | +------------------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('[1, 2, 3]', '$.[1]'); +``` +```sql +SELECT json_extract('[1, 2, 3]', '$.[1]'); +``` +```text +------------------------------------+ | json_extract('[1, 2, 3]', '$.[1]') | +------------------------------------+ | 2 | +------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]'); +``` +```sql +SELECT json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]'); +``` +```text +-------------------------------------------------------------------------------------------------------------------+ | json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2] } }', '$.k1', '$.k2.k21', '$.k2.k22', '$.k2.k22[1]') | +-------------------------------------------------------------------------------------------------------------------+ | ["v1",6.6,[1,2],2] | +-------------------------------------------------------------------------------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name'); +``` +```sql +SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name'); +``` +```text +-----------------------------------------------------------------+ | json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name') | +-----------------------------------------------------------------+ | [null,"doris"] | +-----------------------------------------------------------------+ -1 row in set (0.01 sec) +``` +```sql +SELECT JSON_EXTRACT_ISNULL('{"id": 123, "name": "doris"}', '$.id'); +``` +```text ++----------------------------------------------------------------------------+ +| jsonb_extract_isnull(cast('{"id": 123, "name": "doris"}' as JSON), '$.id') | ++----------------------------------------------------------------------------+ +| 0 | ++----------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_BOOL('{"id": 123, "name": "NULL"}', '$.id'); +``` +```text ++-------------------------------------------------------------------------+ +| jsonb_extract_bool(cast('{"id": 123, "name": "NULL"}' as JSON), '$.id') | ++-------------------------------------------------------------------------+ +| NULL | ++-------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_INT('{"id": 123, "name": "NULL"}', '$.id'); +``` +```text ++------------------------------------------------------------------------+ +| jsonb_extract_int(cast('{"id": 123, "name": "NULL"}' as JSON), '$.id') | ++------------------------------------------------------------------------+ +| 123 | ++------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_INT('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++---------------------------------------------------------------------------+ +| jsonb_extract_int(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++---------------------------------------------------------------------------+ +| NULL | ++---------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXTRACT_STRING('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++------------------------------------------------------------------------------+ +| jsonb_extract_string(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++------------------------------------------------------------------------------+ +| doris | ++------------------------------------------------------------------------------+ ``` -### keywords -JSONB, JSON, json_extract, json_extract_isnull, json_extract_bool, json_extract_int, json_extract_bigint, json_extract_largeint, json_extract_double, json_extract_string diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md index c3fca96a7de..f938c45f2c6 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md @@ -24,18 +24,23 @@ specific language governing permissions and limitations under the License. --> -## json_insert - - ## 描述 -## 语法 +`JSON_INSERT` 函数用于在 JSON 中插入数据并返回结果。 -`VARCHAR json_insert(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## 语法 +```sql +JSON_INSERT (<json_str>, <path>, <val>[, <path>, <val>, ...]) +``` +## 参数 +| 参数 | 描述 | +|-------------|--------------------------------------------------------------------------------------------------------------------------------| +| `<json_str>` | 要插入的 JSON 对象。可以是任意类型元素的 JSON 对象,包括`NULL`,如果没有指定元素,则返回一个空数组。如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误 | +| `<path>` | 要插入的 JSON 路径。如果是 `NULL` ,则返回 NULL | +| `<val>` | 要插入 JSON 的值。如果是 `NULL` ,则会在对应的位置插入 `NULL` 的 value 值。 | -`json_insert` 函数在 JSON 中插入数据并返回结果。如果 `json_str` 或 `path` 为 NULL,则返回 NULL。否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 -路径值对按从左到右的顺序进行评估。 +需要注意的是,路径值对按从左到右的顺序进行评估。 如果 JSON 中不存在该路径,则路径值对会添加该值到 JSON 中,如果路径标识某个类型的值,则: @@ -44,30 +49,47 @@ under the License. 否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 -## 举例 +## 返回值 +返回一个 JSON 值。 +## 示例 +```sql +select json_insert(null, null, null); ``` -MySQL> select json_insert(null, null, null); +```text +---------------------------------+ | json_insert(NULL, NULL, 'NULL') | +---------------------------------+ | NULL | +---------------------------------+ - -MySQL> select json_insert('{"k": 1}', "$.k", 2); +``` +```sql +select json_insert('{"k": 1}', "$.k", 2); +``` +```text +---------------------------------------+ | json_insert('{\"k\": 1}', '$.k', '2') | +---------------------------------------+ | {"k":1} | +---------------------------------------+ - -MySQL> select json_insert('{"k": 1}', "$.j", 2); +``` +```sql +select json_insert('{"k": 1}', "$.j", 2); +``` +```text +---------------------------------------+ | json_insert('{\"k\": 1}', '$.j', '2') | +---------------------------------------+ | {"k":1,"j":2} | +---------------------------------------+ ``` - -### keywords -JSON, json_insert +```sql +select json_insert('{"k": 1}', "$.j", null); +``` +```text ++-----------------------------------------------+ +| json_insert('{"k": 1}', '$.j', 'NULL', '660') | ++-----------------------------------------------+ +| {"k":1,"j":null} | ++-----------------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md index 7d8fbd2253f..82d9dfa36e3 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md @@ -24,47 +24,76 @@ specific language governing permissions and limitations under the License. --> -## json_object ## 描述 + +生成一个或者多个包含指定 Key-Value 对的 json object, 当 Key 值为 NULL 或者传入参数为奇数个时,返回异常错误。 + ## 语法 -`VARCHAR json_object(VARCHAR,...)` +```sql +JSON_OBJECT (<key>, <value>[,<key>, <value>, ...]) +``` + +## 参数 + +| 参数 | 描述 | +|---------|------------------------------------------| +| `<key>` | 指定生成的 json object 的 Key-Value 中的 Key 值 | +| `<value>` | 指定生成的 json object 的 Key-Value 中的 Value 值 | +## 返回值 -生成一个包含指定Key-Value对的json object, 当Key值为NULL或者传入参数为奇数个时,返回异常错误 +返回一个 json object。特殊情况如下: +* 如果没有传入参数,返回一个空的 json object。 +* 如果传入的参数个数为奇数个,返回异常错误。 +* 如果传入的 Key 为 NULL,返回异常错误。 +* 如果传入的 Value 为 NULL,返回的 json object 中该 Key-Value 对的 Value 值为 NULL。 -## 举例 +## 示例 +```sql +select json_object(); ``` -MySQL> select json_object(); +```text +---------------+ | json_object() | +---------------+ | {} | +---------------+ - -MySQL> select json_object('time',curtime()); +``` +```sql +select json_object('time',curtime()); +``` +```text +--------------------------------+ | json_object('time', curtime()) | +--------------------------------+ | {"time": "10:49:18"} | +--------------------------------+ - - -MySQL> SELECT json_object('id', 87, 'name', 'carrot'); +``` +```sql +SELECT json_object('id', 87, 'name', 'carrot'); +``` +```text +-----------------------------------------+ | json_object('id', 87, 'name', 'carrot') | +-----------------------------------------+ | {"id": 87, "name": "carrot"} | +-----------------------------------------+ - - -MySQL> select json_object('username',null); +``` +```sql +select json_object('username',null); +``` +```text +---------------------------------+ | json_object('username', 'NULL') | +---------------------------------+ | {"username": NULL} | +---------------------------------+ ``` -### keywords -json,object,json_object +```sql +select json_object(null,null); +``` +```text +ERROR 1105 (HY000): errCode = 2, detailMessage = json_object key can't be NULL: json_object(NULL) +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md index 944ccd09e1d..016437a8021 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md @@ -24,45 +24,70 @@ specific language governing permissions and limitations under the License. --> -## json_replace - ## 描述 +`JSON_REPLACE` 函数用于在 JSON 中更新数据并返回结果。 + ## 语法 +```sql +JSON_REPLACE (<json_str>, <path>, <val>[, <jsonPath>, <val>, ...]) +``` +## 参数 +| 参数 | 描述 | +|--------------|---------------------------------------------------------------------------------------------| +| `<json_str>` | 要替换的 JSON 数据。可以是任意类型元素的 JSON 对象,包括`NULL`,如果没有指定元素,则返回一个空数组。如果 `json_str` 不是有效的 JSON,则会返回错误 | +| `<path>` | 要替换的 JSON 路径。 | +| `<val>` | 要替换 JSON_PATH Key 对应 value 的值。如果是 `NULL` ,则会在对应的位置插入 `NULL` 的 value 值。 | + -`VARCHAR json_replace(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## 返回值 +如果 `json_str` 和 `path` 都为 NULL,则返回 NULL。 -`json_replace` 函数在 JSON 中更新数据并返回结果。如果 `json_str` 或 `path` 为 NULL,则返回 NULL。否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 +否则,如果 `json_str` 不是有效的 JSON 或任何 `path` 参数不是有效的路径表达式或包含了 * 通配符,则会返回错误。 路径值对按从左到右的顺序进行评估。 -如果 JSON 中已存在某个路径,则路径值对会将现有 JSON 值覆盖为新值。 -否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 +如果 JSON 中已存在某个路径,则路径值对会将现有 JSON 值覆盖为新值。否则,对于 JSON 中不存在的某个路径的路径值对将被忽略且不会产生任何影响。 -## 举例 +## 示例 +```sql +select json_replace(null, null, null); ``` -MySQL> select json_replace(null, null, null); +```text +----------------------------------+ | json_replace(NULL, NULL, 'NULL') | +----------------------------------+ | NULL | +----------------------------------+ - -MySQL> select json_replace('{"k": 1}', "$.k", 2); +``` +```sql +select json_replace('{"k": 1}', "$.k", 2); +``` +```text +----------------------------------------+ | json_replace('{\"k\": 1}', '$.k', '2') | +----------------------------------------+ | {"k":2} | +----------------------------------------+ - -MySQL> select json_replace('{"k": 1}', "$.j", 2); +``` +```sql +select json_replace('{"k": 1}', "$.j", 2); +``` +```text +----------------------------------------+ | json_replace('{\"k\": 1}', '$.j', '2') | +----------------------------------------+ | {"k":1} | +----------------------------------------+ ``` - -### keywords -JSON, json_replace +```sql +select json_replace(null, null, 's'); +``` +```text ++--------------------------------------+ +| json_replace(NULL, NULL, 's', '006') | ++--------------------------------------+ +| NULL | ++--------------------------------------+ +``` diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md index c7eeb0f44fe..8120d320d65 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md @@ -24,13 +24,8 @@ specific language governing permissions and limitations under the License. --> -## json_unquote ## 描述 -## 语法 - -`VARCHAR json_unquote(VARCHAR)` - -这个函数将去掉JSON值中的引号,并将结果作为utf8mb4字符串返回。如果参数为NULL,则返回NULL。 +这个函数将去掉 JSON 值中的引号,并将结果作为 utf8mb4 字符串返回。如果参数为 NULL,则返回 NULL。 在字符串中显示的如下转义序列将被识别,对于所有其他转义序列,反斜杠将被忽略。 @@ -46,38 +41,91 @@ under the License. | \uxxxx | Unicode 值 XXXX 的 UTF-8 字节 | +## 语法 +```sql +JSON_UNQUOTE (<a>) +``` -## 举例 +## 参数 +| 参数 | 描述 | +|------|--------------------------------------------------------| +| `<a>` | 要去除引号的元素。 | + +## 返回值 +返回一个 utf8mb4 字符串。特殊情况如下: +* 如果传入的参数为 NULL,返回 NULL。 +* 如果传入的参数不是一个带有双引号的值,则会返回值本身。 +* 如果传入的参数不是一个字符串,则会被自动转换为字符串后,返回值本身。 +## 举例 +```sql +SELECT json_unquote('"doris"'); ``` -mysql> SELECT json_unquote('"doris"'); + +```text +-------------------------+ | json_unquote('"doris"') | +-------------------------+ | doris | +-------------------------+ - -mysql> SELECT json_unquote('[1, 2, 3]'); +``` +```sql +SELECT json_unquote('[1, 2, 3]'); +``` +```text +---------------------------+ | json_unquote('[1, 2, 3]') | +---------------------------+ | [1, 2, 3] | +---------------------------+ - - -mysql> SELECT json_unquote(null); +``` +```sql +SELECT json_unquote(null); +``` +```text +--------------------+ | json_unquote(NULL) | +--------------------+ | NULL | +--------------------+ - -mysql> SELECT json_unquote('"\\ttest"'); +``` +```sql +SELECT json_unquote('"\\ttest"'); +``` +```text +--------------------------+ | json_unquote('"\ttest"') | +--------------------------+ | test | +--------------------------+ ``` -### keywords -json,unquote,json_unquote +```sql +select json_unquote('"doris'); +``` +```text ++------------------------+ +| json_unquote('"doris') | ++------------------------+ +| "doris | ++------------------------+ +``` +```sql +select json_unquote('doris'); +``` +```text ++-----------------------+ +| json_unquote('doris') | ++-----------------------+ +| doris | ++-----------------------+ +``` +```sql +select json_unquote(1); +``` +```text ++-----------------------------------------+ +| json_unquote(cast(1 as VARCHAR(65533))) | ++-----------------------------------------+ +| 1 | ++-----------------------------------------+ +``` --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org