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 bec1850914e [doc] Fix json_fun_* en_doc (#1862) bec1850914e is described below commit bec1850914e97ee6e1834b969b0ffcba9aef7bca Author: mklzl <36362816+mk...@users.noreply.github.com> AuthorDate: Tue Jan 21 17:18:50 2025 +0800 [doc] Fix json_fun_* en_doc (#1862) "json_array json_object json_quote json_unquote json_extract json_insert json_replace json_set json_exists_path jsonb_extract json_parse " ## Versions - [x] dev - [x] 3.0 - [x] 2.1 - [ ] 2.0 ## Languages - [ ] Chinese - [x] English ## Docs Checklist - [ ] Checked by AI - [ ] Test Cases Built --- .../scalar-functions/json-functions/json-array.md | 50 ++++-- .../json-functions/json-exists-path.md | 54 +++++- .../json-functions/json-extract.md | 192 ++++++++++++-------- .../scalar-functions/json-functions/json-insert.md | 52 ++++-- .../scalar-functions/json-functions/json-object.md | 63 +++++-- .../scalar-functions/json-functions/json-parse.md | 128 +++++++++++--- .../scalar-functions/json-functions/json-quote.md | 50 ++++-- .../json-functions/json-replace.md | 51 ++++-- .../scalar-functions/json-functions/json-set.md | 42 +++-- .../json-functions/json-unquote.md | 83 +++++++-- .../scalar-functions/json-functions/json-array.md | 49 ++++-- .../json-functions/json-exists-path.md | 55 +++++- .../json-functions/json-extract.md | 193 +++++++++++++-------- .../scalar-functions/json-functions/json-insert.md | 52 ++++-- .../scalar-functions/json-functions/json-object.md | 63 +++++-- .../scalar-functions/json-functions/json-parse.md | 129 ++++++++++---- .../scalar-functions/json-functions/json-quote.md | 50 ++++-- .../json-functions/json-replace.md | 50 ++++-- .../scalar-functions/json-functions/json-set.md | 42 +++-- .../json-functions/json-unquote.md | 83 +++++++-- .../json-functions/jsonb-extract.md | 25 --- .../scalar-functions/json-functions/json-array.md | 49 ++++-- .../json-functions/json-exists-path.md | 54 +++++- .../json-functions/json-extract.md | 193 +++++++++++++-------- .../scalar-functions/json-functions/json-insert.md | 53 ++++-- .../scalar-functions/json-functions/json-object.md | 62 +++++-- .../scalar-functions/json-functions/json-parse.md | 129 ++++++++++---- .../scalar-functions/json-functions/json-quote.md | 50 ++++-- .../json-functions/json-replace.md | 50 ++++-- .../scalar-functions/json-functions/json-set.md | 44 +++-- .../json-functions/json-unquote.md | 83 +++++++-- .../json-functions/jsonb-extract.md | 25 --- versioned_sidebars/version-2.1-sidebars.json | 1 - versioned_sidebars/version-3.0-sidebars.json | 1 - 34 files changed, 1668 insertions(+), 682 deletions(-) diff --git a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md index 5a038c24cfe..0abb7a93587 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md @@ -24,47 +24,71 @@ specific language governing permissions and limitations under the License. --> -## json_array -### Description -#### Syntax +## Description +Generate a json array containing the specified values, return empty if no values + -`VARCHAR json_array(VARCHAR,...)` +## Syntax +```sql +JSON_ARRAY (<a>, ...) +``` +## Parameters +| Parameter | Description | +|------|---------------------------------------------------------------------------------------------------------------| +| `<a>, ...` | Elements to be included in the JSON array. It can be a single or multiple values of any type, including NULL. | -Generate a json array containing the specified values, return empty if no values -### example +## Return Values +Returns a JSON array containing the specified values. If no values are specified, an empty JSON array is returned. + +## Examples + +```sql +select json_array(); ``` -MySQL> select json_array(); + +```text +--------------+ | json_array() | +--------------+ | [] | +--------------+ +``` -MySQL> select json_array(null); +```sql +select json_array(null); +``` + +```text +--------------------+ | json_array('NULL') | +--------------------+ | [NULL] | +--------------------+ +``` +```sql +SELECT json_array(1, "abc", NULL, TRUE, CURTIME()); +``` - -MySQL> SELECT json_array(1, "abc", NULL, TRUE, CURTIME()); +```text +-----------------------------------------------+ | json_array(1, 'abc', 'NULL', TRUE, curtime()) | +-----------------------------------------------+ | [1, "abc", NULL, TRUE, "10:41:15"] | +-----------------------------------------------+ +``` +```sql +select json_array("a", null, "c"); +``` -MySQL> select json_array("a", null, "c"); +```text +------------------------------+ | json_array('a', 'NULL', 'c') | +------------------------------+ | ["a", NULL, "c"] | +------------------------------+ ``` -### keywords -json,array,json_array + diff --git a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md index dcea0736f4e..d6ca8ebe44d 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md @@ -1,3 +1,4 @@ + --- { "title": "JSON_EXISTS_PATH", @@ -24,23 +25,60 @@ specific language governing permissions and limitations under the License. --> -## json_exists_path -### description +## Description It is used to judge whether the field specified by json_path exists in the JSON data. If it exists, it returns TRUE, and if it does not exist, it returns FALSE -#### Syntax +## Syntax ```sql -BOOLEAN json_exists_path(JSON j, VARCHAR json_path) +JSON_EXISTS_PATH (<json_str>, <path>) ``` -### example +## Alias + +* JSONB_EXISTS_PATH + +## Parameters +| Parameter | Description | +|--------------|--------------------------------------------------------| +| `<json_str>` | The element to be included in the JSON array. It can be a value of any type, including NULL. If no element is specified, an empty array is returned. +| `<path>` | The JSON path to be judged. If it is NULL, then return NULL. | -Refer to [json tutorial](../../sql-reference/Data-Types/JSON.md) +## Return Values +If it exists, return TRUE; if it does not exist, return FALSE. -### keywords +## Examples -json_exists_path +```sql +SELECT JSON_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++---------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++---------------------------------------------------------------------------+ +| 1 | ++---------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.age'); +``` +```text ++--------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.age') | ++--------------------------------------------------------------------------+ +| 0 | ++--------------------------------------------------------------------------+ +``` +```sql +SELECT JSONB_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.age'); +``` +```text ++--------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.age') | ++--------------------------------------------------------------------------+ +| 0 | ++--------------------------------------------------------------------------+ +``` diff --git a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md index 39aaf61a8e8..01b104483ec 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md @@ -24,35 +24,61 @@ specific language governing permissions and limitations under the License. --> -## json_extract - -### description - -#### Syntax +## Description +JSON_EXTRACT is a series of functions that extract the field specified by json_path from JSON data and provide different series of functions according to the type of the field to be extracted. + +* JSON_EXTRACT returns the VARCHAR type for a json string of the VARCHAR type. +* JSON_EXTRACT_ISNULL returns the BOOLEAN type indicating whether it is a json null. +* JSON_EXTRACT_BOOL returns the BOOLEAN type. +* JSON_EXTRACT_INT returns the INT type. +* JSON_EXTRACT_BIGINT returns the BIGINT type. +* JSON_EXTRACT_LARGEINT returns the LARGEINT type. +* JSON_EXTRACT_DOUBLE returns the DOUBLE type. +* JSON_EXTRACT_STRING returns the STRING type. + +## Alias +* JSONB_EXTRACT is the same as JSON_EXTRACT. +* JSONB_EXTRACT_ISNULL is the same as JSON_EXTRACT_ISNULL. +* JSONB_EXTRACT_BOOL is the same as JSON_EXTRACT_BOOL. +* JSONB_EXTRACT_INT is the same as JSON_EXTRACT_INT. +* JSONB_EXTRACT_BIGINT is the same as JSON_EXTRACT_BIGINT. +* JSONB_EXTRACT_LARGEINT is the same as JSON_EXTRACT_LARGEINT. +* JSONB_EXTRACT_DOUBLE is the same as JSON_EXTRACT_DOUBLE. +* JSONB_EXTRACT_STRING is the same as JSON_EXTRACT_STRING. + +## Syntax ```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>) +``` +Alias functions have the same syntax and usage as the above functions, except for the function names. -json_extract functions extract field specified by json_path from JSON. A series of functions are provided for different datatype. -- json_extract with VARCHAR argument, extract and return VARCHAR datatype -- jsonb_extract extract and return JSON datatype -- json_extract_isnull check if the field is json null and return BOOLEAN datatype -- json_extract_bool extract and return BOOLEAN datatype -- json_extract_int extract and return INT datatype -- json_extract_bigint extract and return BIGINT datatype -- json_extract_largeint extract and return LARGEINT datatype -- json_extract_double extract and return DOUBLE datatype -- json_extract_STRING extract and return STRING datatype - +## Parameters +| Parameter | Description | +|--------------|-----------------------------| +| `<json_str>` | The JSON-type parameter or field to be extracted. | +| `<path>` | The JSON path to extract the target element from the target JSON. | json path syntax: - '$' for json document root - '.k1' for element of json object with key 'k1' @@ -60,73 +86,103 @@ json path syntax: - '[i]' for element of json array at index i - Use '$[last]' to get the last element of json_array, and '$[last-1]' to get the penultimate element, and so on. +## Return Values +According to the type of the field to be extracted, return the data type of the specified JSON_PATH in the target JSON. Special case handling is as follows: +* If the field specified by json_path does not exist in the JSON, return NULL. +* If the actual type of the field specified by json_path in the JSON is inconsistent with the type specified by json_extract_t. +* if it can be losslessly converted to the specified type, return the specified type t; if not, return NULL. -Exception handling is as follows: -- if the field specified by json_path does not exist, return NULL -- if datatype of the field specified by json_path is not the same with type of json_extract_t, return t if it can be cast to t else NULL -## json_exists_path and json_type -### description - -#### Syntax - +## Examples ```sql -BOOLEAN json_exists_path(JSON j, VARCHAR json_path) -STRING json_type(JSON j, VARCHAR json_path) +SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); ``` -There are two extra functions to check field existence and type -- json_exists_path check the existence of the field specified by json_path, return TRUE or FALS -- json_type get the type as follows of the field specified by json_path, return NULL if it does not exist - - object - - array - - null - - bool - - int - - bigint - - largeint - - double - - string - -### example - -refer to [json tutorial](../../sql-reference/Data-Types/JSON.md) for more. - -``` -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, json_exists_path, json_type \ No newline at end of file +```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/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md index 0b8b8770f08..94ffe338feb 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md @@ -24,13 +24,21 @@ specific language governing permissions and limitations under the License. --> -## json_insert +## Description +The JSON_INSERT function is used to insert data into JSON and return the result. -### Description -#### Syntax -`VARCHAR json_insert(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## Syntax +```sql +JSON_INSERT (<json_str>, <path>, <val>[, <path>, <val>, ...]) +``` +## Parameters +| Parameter | Description | +|-------------|--------------------------------------------------------------------------------------------------------------------------------| +| `<json_str>` | The JSON object to be inserted. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON or any path parameter is not a valid path expression or contains a * wildcard, an error is returned. | +| `<path>` | The JSON path to be inserted. If it is NULL, then return NULL. | +| `<val>` | The value to be inserted into the JSON. If it is NULL, then a NULL value will be inserted at the corresponding position. | `json_insert` function inserts data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -44,30 +52,48 @@ A path-value pair for a nonexisting path in the json adds the value to the json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +## Return Values +Returns a JSON value. + +### Examples +```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/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md index 33199db20a4..4437b915882 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md @@ -24,48 +24,75 @@ specific language governing permissions and limitations under the License. --> -## json_object -### Description -#### Syntax +## Description +Generate a json object containing the specified Key-Value, +an exception error is returned when Key is NULL or the number of parameters are odd. -`VARCHAR json_object(VARCHAR,...)` +## Syntax +```sql +JSON_OBJECT (<key>, <value>[,<key>, <value>, ...]) +``` +## Parameters -Generate a json object containing the specified Key-Value, -an exception error is returned when Key is NULL or the number of parameters are odd. +| Parameter | Description | +|---------|------------------------------------------| +| `<key>` | The Key value in the Key-Value of the generated json object. | +| `<value>` | The Value value in the Key-Value of the generated json object. | | -### example +## Return Values +Return a json object. Special cases are as follows: +* If no parameters are passed, return an empty json object. +* If the number of parameters passed is odd, return an exception error. +* If the passed Key is NULL, return an exception error. +* If the passed Value is NULL, the Value value of the Key-Value pair in the returned json object is NULL. +## Examples + +```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/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md index 78ac813a628..c0d62a7c7e3 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md @@ -24,61 +24,133 @@ specific language governing permissions and limitations under the License. --> -## json_parse -### description -#### Syntax + +## Description +Parse the original JSON string into JSON binary format. To meet the needs of different abnormal data processing, different JSON_PARSE series functions are provided as follows: +* JSON_PARSE: Parse the JSON string, and report an error when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_INVALID: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_NULL: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_VALUE: Parse the JSON string, and return the default value specified by the parameter default_json_str when the input string is not a valid JSON string. +* JSON_PARSE_NOTNULL: Parse the JSON string, and return NULL when the input string is not a valid JSON string. + +## Alias +* JSONB_PARSE is the same as JSON_PARSE +* JSONB_PARSE_ERROR_TO_INVALID is the same as JSON_PARSE_ERROR_TO_INVALID +* JSONB_PARSE_ERROR_TO_NULL is the same as JSON_PARSE_ERROR_TO_NULL +* JSONB_PARSE_ERROR_TO_VALUE is the same as JSON_PARSE_ERROR_TO_VALUE +* JSONB_PARSE_NOTNULL is the same as JSON_PARSE_NOTNULL + +## Syntax ```sql -JSON json_parse(VARCHAR json_str) -JSON json_parse_error_to_null(VARCHAR json_str) -JSON json_parse_error_to_value(VARCHAR json_str, VARCHAR default_json_str) +JSON_PARSE (<json_str>) +``` +```sql +JSON_PARSE_ERROR_TO_INVALID (<json_str>) +``` +```sql +JSON_PARSE_ERROR_TO_NULL (<json_str>) ``` +```sql +JSON_PARSE_ERROR_TO_VALUE (<json_str>, <default_json_str>) +``` +```sql +JSONB_PARSE_NOTNULL (<json_str>) +``` + +## Parameters +| Parameter | Description | +|--------------|-----------------------------| +| `<json_str>` | The JSON type parameter or field to be extracted. | +| `<default_json_str>` | When the input string is not a valid JSON string, return the default value specified by the parameter default_json_str. | | + +## Return Values json_parse functions parse JSON string to binary format. A series of functions are provided to satisfy different demand for exception handling. - all return NULL if json_str is NULL - if json_str is not valid - json_parse will report error + - json_parse_error_to_invalid will return NULL - json_parse_error_to_null will return NULL - json_parse_error_to_value will return the value specified by default_json_str + - json_parse_notnull will return NULL -### example - -1. parse valid JSON string - +### Examples +1. Parse valid JSON string +```sql +SELECT json_parse('{"k1":"v31","k2":300}'); ``` -mysql> SELECT json_parse('{"k1":"v31","k2":300}'); +```text +--------------------------------------+ | json_parse('{"k1":"v31","k2":300}') | +--------------------------------------+ | {"k1":"v31","k2":300} | +--------------------------------------+ -1 row in set (0.01 sec) ``` - -2. parse invalid JSON string - +```sql +SELECT json_parse_error_to_invalid('{"k1":"v31","k2":300}'); ``` -mysql> SELECT json_parse('invalid json'); +```text ++-------------------------------------------------------+ +| jsonb_parse_error_to_invalid('{"k1":"v31","k2":300}') | ++-------------------------------------------------------+ +| {"k1":"v31","k2":300} | ++-------------------------------------------------------+ +``` +```sql +SELECT json_parse_notnull('{"a":"b"}'); +``` +```text ++----------------------------------+ +| jsonb_parse_notnull('{"a":"b"}') | ++----------------------------------+ +| {"a":"b"} | ++----------------------------------+ +``` +```sql +SELECT json_parse_error_to_value('{"k1":"v31","k2":300}','{}'); +``` +```text ++-----------------------------------------------------------+ +| jsonb_parse_error_to_value('{"k1":"v31","k2":300}', '{}') | ++-----------------------------------------------------------+ +| {"k1":"v31","k2":300} | ++-----------------------------------------------------------+ +``` +2. Parse invalid JSON string +```sql +SELECT json_parse('invalid json'); +``` +```text ERROR 1105 (HY000): errCode = 2, detailMessage = json parse error: Invalid document: document must be an object or an array for value: invalid json - -mysql> SELECT json_parse_error_to_null('invalid json'); +``` +```sql +SELECT json_parse_error_to_invalid('invalid json'); +``` +```text ++----------------------------------------------+ +| jsonb_parse_error_to_invalid('invalid json') | ++----------------------------------------------+ +| NULL | ++----------------------------------------------+ +``` +```sql +SELECT json_parse_notnull('invalid json'); +``` +```text +-------------------------------------------+ -| json_parse_error_to_null('invalid json') | +| jsonb_parse_error_to_null('invalid json') | +-------------------------------------------+ | NULL | +-------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_parse_error_to_value('invalid json', '{}'); +``` +```sql +SELECT json_parse_error_to_value('invalid json', '{}'); +``` +```text +--------------------------------------------------+ | json_parse_error_to_value('invalid json', '{}') | +--------------------------------------------------+ | {} | +--------------------------------------------------+ -1 row in set (0.00 sec) ``` - -refer to json tutorial for more. - -### keywords -JSONB, JSON, json_parse, json_parse_error_to_null, json_parse_error_to_value \ No newline at end of file diff --git a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md index 16a3f35f1a4..1561509dd58 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md @@ -24,47 +24,63 @@ specific language governing permissions and limitations under the License. --> -## json_quote -### Description -#### Syntax +## Description +Enclose json_value in double quotes ("), escape special characters contained. + +## Syntax +```sql +JSON_QUOTE (<a>) +``` -`VARCHAR json_quote(VARCHAR)` +## Parameters +| Parameter | Description | +|-----------|------------------------------------------| +| `<a>` | The value of the json_value to be enclosed. | -Enclose json_value in double quotes ("), escape special characters contained. -### example +## Return Values +Return a json_value. Special cases are as follows: +* If the passed parameter is NULL, return NULL. +### Examples +```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/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md index f019679a759..10ff4f365a9 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md @@ -24,13 +24,23 @@ specific language governing permissions and limitations under the License. --> -## json_replace -### Description -#### Syntax +## Description +The JSON_REPLACE function is used to update data in a JSON and return the result. -`VARCHAR json_replace(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## Syntax +```sql +JSON_REPLACE (<json_str>, <path>, <val>[, <jsonPath>, <val>, ...]) +``` + +## Parameters +| Parameter | Description | +|--------------|---------------------------------------------------------------------------------------------| +| `<json_str>` | The JSON data to be replaced. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON, an error will be returned. | +| `<path>` | The JSON path to be replaced. | +| `<val>` | The value to replace the value corresponding to the JSON_PATH Key. If it is NULL, then a NULL value will be inserted at the corresponding position. | +## Return Values `json_replace` function updates data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -40,30 +50,45 @@ A path-value pair for an existing path in the json overwrites the existing json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +### Examples +```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/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md index 2e0d80cf34f..12dbff80d9a 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md @@ -24,13 +24,24 @@ specific language governing permissions and limitations under the License. --> -## json_set +## Description +The json_set function inserts or updates data in a JSON and returns the result. -### Description -#### Syntax +## Syntax +```sql +JSON_SET (<json_str>, <path>, <val> [, <path>, <val>, ...]) +``` + +## Parameters -`VARCHAR json_set(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +| Parameter | Description | +|-------|-------------------------------------------------------------------------------------------------------------------------| +| `<jsonStr>` | The JSON object to be inserted. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON or any path parameter is not a valid path expression or contains a * wildcard, an error is returned. | +| `<jsonPath>` | The JSON path to be inserted. If it is NULL, then return NULL. | +| `<val>` | The value to be inserted into the JSON. If it is NULL, then a NULL value will be inserted at the corresponding position. | +## Return Values +Returns a JSON value. `json_set` function inserts or updates data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -44,30 +55,35 @@ A path-value pair for an existing path in the json overwrites the existing json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +## Examples +```sql +select json_set(null, null, null); ``` -MySQL> select json_set(null, null, null); +```text +------------------------------+ | json_set(NULL, NULL, 'NULL') | +------------------------------+ | NULL | +------------------------------+ - -MySQL> select json_set('{"k": 1}', "$.k", 2); +``` +```sql +select json_set('{"k": 1}', "$.k", 2); +``` +```text +------------------------------------+ | json_set('{\"k\": 1}', '$.k', '2') | +------------------------------------+ | {"k":2} | +------------------------------------+ - -MySQL> select json_set('{"k": 1}', "$.j", 2); +``` +```sql +select json_set('{"k": 1}', "$.j", 2); +``` +```text +------------------------------------+ | json_set('{\"k\": 1}', '$.j', '2') | +------------------------------------+ | {"k":1,"j":2} | +------------------------------------+ ``` - -### keywords -JSON, json_set diff --git a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md index 4a059b68d79..c7be5654f5e 100644 --- a/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md +++ b/docs/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md @@ -24,13 +24,24 @@ specific language governing permissions and limitations under the License. --> -## json_unquote -### Description -#### Syntax +## Description +This function unquotes a JSON value and returns the result as a utf8mb4 string. If the argument is NULL, it will return NULL. -`VARCHAR json_unquote(VARCHAR)` +## Syntax +```sql +JSON_UNQUOTE (<a>) +``` +## Parameters +| Parameters | Description | +|------|-------------------------------------------------------| +| `<a>` | The element to be unquoted. | -This function unquotes a JSON value and returns the result as a utf8mb4 string. If the argument is NULL, it will return NULL. +## Return Values + +Returns a utf8mb4 string. Special cases are as follows: +* If the passed parameter is NULL, return NULL. +* If the passed parameter is not a value with double quotes, the value itself will be returned. +* If the passed parameter is not a string, it will be automatically converted to a string and then the value itself will be returned. Escape sequences within a string as shown in the following table will be recognized. Backslashes will be ignored for all other escape sequences. @@ -46,38 +57,76 @@ Escape sequences within a string as shown in the following table will be recogni | \uxxxx | UTF-8 bytes for Unicode value XXXX | +### Examples -### example - +```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/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md index 5a038c24cfe..119fec62a01 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md @@ -24,47 +24,70 @@ specific language governing permissions and limitations under the License. --> -## json_array -### Description -#### Syntax +## Description +Generate a json array containing the specified values, return empty if no values + -`VARCHAR json_array(VARCHAR,...)` +## Syntax +```sql +JSON_ARRAY (<a>, ...) +``` +## Parameters +| Parameter | Description | +|------|---------------------------------------------------------------------------------------------------------------| +| `<a>, ...` | Elements to be included in the JSON array. It can be a single or multiple values of any type, including NULL. | -Generate a json array containing the specified values, return empty if no values -### example +## Return Values +Returns a JSON array containing the specified values. If no values are specified, an empty JSON array is returned. + +## Examples + +```sql +select json_array(); ``` -MySQL> select json_array(); + +```text +--------------+ | json_array() | +--------------+ | [] | +--------------+ +``` -MySQL> select json_array(null); +```sql +select json_array(null); +``` + +```text +--------------------+ | json_array('NULL') | +--------------------+ | [NULL] | +--------------------+ +``` +```sql +SELECT json_array(1, "abc", NULL, TRUE, CURTIME()); +``` - -MySQL> SELECT json_array(1, "abc", NULL, TRUE, CURTIME()); +```text +-----------------------------------------------+ | json_array(1, 'abc', 'NULL', TRUE, curtime()) | +-----------------------------------------------+ | [1, "abc", NULL, TRUE, "10:41:15"] | +-----------------------------------------------+ +``` +```sql +select json_array("a", null, "c"); +``` -MySQL> select json_array("a", null, "c"); +```text +------------------------------+ | json_array('a', 'NULL', 'c') | +------------------------------+ | ["a", NULL, "c"] | +------------------------------+ ``` -### keywords -json,array,json_array diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md index dcea0736f4e..6677fb266da 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md @@ -24,23 +24,60 @@ specific language governing permissions and limitations under the License. --> -## json_exists_path - -### description +## Description It is used to judge whether the field specified by json_path exists in the JSON data. If it exists, it returns TRUE, and if it does not exist, it returns FALSE -#### Syntax +## Syntax ```sql -BOOLEAN json_exists_path(JSON j, VARCHAR json_path) +JSON_EXISTS_PATH (<json_str>, <path>) ``` -### example +## Alias + +* JSONB_EXISTS_PATH + + +## Parameters +| Parameter | Description | +|--------------|--------------------------------------------------------| +| `<json_str>` | The element to be included in the JSON array. It can be a value of any type, including NULL. If no element is specified, an empty array is returned. +| `<path>` | The JSON path to be judged. If it is NULL, then return NULL. | -Refer to [json tutorial](../../sql-reference/Data-Types/JSON.md) +## Return Values +If it exists, return TRUE; if it does not exist, return FALSE. -### keywords +## Examples -json_exists_path +```sql +SELECT JSON_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++---------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++---------------------------------------------------------------------------+ +| 1 | ++---------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.age'); +``` +```text ++--------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.age') | ++--------------------------------------------------------------------------+ +| 0 | ++--------------------------------------------------------------------------+ +``` +```sql +SELECT JSONB_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.age'); +``` +```text ++--------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.age') | ++--------------------------------------------------------------------------+ +| 0 | ++--------------------------------------------------------------------------+ +``` diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md index 39aaf61a8e8..2c29d721558 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md @@ -24,35 +24,60 @@ specific language governing permissions and limitations under the License. --> -## json_extract - -### description - -#### Syntax - +## Description +JSON_EXTRACT is a series of functions that extract the field specified by json_path from JSON data and provide different series of functions according to the type of the field to be extracted. + +* JSON_EXTRACT returns the VARCHAR type for a json string of the VARCHAR type. +* JSON_EXTRACT_ISNULL returns the BOOLEAN type indicating whether it is a json null. +* JSON_EXTRACT_BOOL returns the BOOLEAN type. +* JSON_EXTRACT_INT returns the INT type. +* JSON_EXTRACT_BIGINT returns the BIGINT type. +* JSON_EXTRACT_LARGEINT returns the LARGEINT type. +* JSON_EXTRACT_DOUBLE returns the DOUBLE type. +* JSON_EXTRACT_STRING returns the STRING type. + +## Alias +* JSONB_EXTRACT is the same as JSON_EXTRACT. +* JSONB_EXTRACT_ISNULL is the same as JSON_EXTRACT_ISNULL. +* JSONB_EXTRACT_BOOL is the same as JSON_EXTRACT_BOOL. +* JSONB_EXTRACT_INT is the same as JSON_EXTRACT_INT. +* JSONB_EXTRACT_BIGINT is the same as JSON_EXTRACT_BIGINT. +* JSONB_EXTRACT_LARGEINT is the same as JSON_EXTRACT_LARGEINT. +* JSONB_EXTRACT_DOUBLE is the same as JSON_EXTRACT_DOUBLE. +* JSONB_EXTRACT_STRING is the same as JSON_EXTRACT_STRING. + +## Syntax ```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>) +``` +Alias functions have the same syntax and usage as the above functions, except for the function names. -json_extract functions extract field specified by json_path from JSON. A series of functions are provided for different datatype. -- json_extract with VARCHAR argument, extract and return VARCHAR datatype -- jsonb_extract extract and return JSON datatype -- json_extract_isnull check if the field is json null and return BOOLEAN datatype -- json_extract_bool extract and return BOOLEAN datatype -- json_extract_int extract and return INT datatype -- json_extract_bigint extract and return BIGINT datatype -- json_extract_largeint extract and return LARGEINT datatype -- json_extract_double extract and return DOUBLE datatype -- json_extract_STRING extract and return STRING datatype - +## Parameters +| Parameter | Description | +|--------------|-----------------------------| +| `<json_str>` | The JSON-type parameter or field to be extracted. | +| `<path>` | The JSON path to extract the target element from the target JSON. | json path syntax: - '$' for json document root - '.k1' for element of json object with key 'k1' @@ -60,73 +85,103 @@ json path syntax: - '[i]' for element of json array at index i - Use '$[last]' to get the last element of json_array, and '$[last-1]' to get the penultimate element, and so on. +## Return Values +According to the type of the field to be extracted, return the data type of the specified JSON_PATH in the target JSON. Special case handling is as follows: +* If the field specified by json_path does not exist in the JSON, return NULL. +* If the actual type of the field specified by json_path in the JSON is inconsistent with the type specified by json_extract_t. +* if it can be losslessly converted to the specified type, return the specified type t; if not, return NULL. -Exception handling is as follows: -- if the field specified by json_path does not exist, return NULL -- if datatype of the field specified by json_path is not the same with type of json_extract_t, return t if it can be cast to t else NULL - - -## json_exists_path and json_type -### description -#### Syntax +## Examples ```sql -BOOLEAN json_exists_path(JSON j, VARCHAR json_path) -STRING json_type(JSON j, VARCHAR json_path) +SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); ``` -There are two extra functions to check field existence and type -- json_exists_path check the existence of the field specified by json_path, return TRUE or FALS -- json_type get the type as follows of the field specified by json_path, return NULL if it does not exist - - object - - array - - null - - bool - - int - - bigint - - largeint - - double - - string - -### example - -refer to [json tutorial](../../sql-reference/Data-Types/JSON.md) for more. - -``` -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, json_exists_path, json_type \ No newline at end of file +```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/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md index 0b8b8770f08..94ffe338feb 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md @@ -24,13 +24,21 @@ specific language governing permissions and limitations under the License. --> -## json_insert +## Description +The JSON_INSERT function is used to insert data into JSON and return the result. -### Description -#### Syntax -`VARCHAR json_insert(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## Syntax +```sql +JSON_INSERT (<json_str>, <path>, <val>[, <path>, <val>, ...]) +``` +## Parameters +| Parameter | Description | +|-------------|--------------------------------------------------------------------------------------------------------------------------------| +| `<json_str>` | The JSON object to be inserted. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON or any path parameter is not a valid path expression or contains a * wildcard, an error is returned. | +| `<path>` | The JSON path to be inserted. If it is NULL, then return NULL. | +| `<val>` | The value to be inserted into the JSON. If it is NULL, then a NULL value will be inserted at the corresponding position. | `json_insert` function inserts data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -44,30 +52,48 @@ A path-value pair for a nonexisting path in the json adds the value to the json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +## Return Values +Returns a JSON value. + +### Examples +```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/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md index 33199db20a4..4437b915882 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md @@ -24,48 +24,75 @@ specific language governing permissions and limitations under the License. --> -## json_object -### Description -#### Syntax +## Description +Generate a json object containing the specified Key-Value, +an exception error is returned when Key is NULL or the number of parameters are odd. -`VARCHAR json_object(VARCHAR,...)` +## Syntax +```sql +JSON_OBJECT (<key>, <value>[,<key>, <value>, ...]) +``` +## Parameters -Generate a json object containing the specified Key-Value, -an exception error is returned when Key is NULL or the number of parameters are odd. +| Parameter | Description | +|---------|------------------------------------------| +| `<key>` | The Key value in the Key-Value of the generated json object. | +| `<value>` | The Value value in the Key-Value of the generated json object. | | -### example +## Return Values +Return a json object. Special cases are as follows: +* If no parameters are passed, return an empty json object. +* If the number of parameters passed is odd, return an exception error. +* If the passed Key is NULL, return an exception error. +* If the passed Value is NULL, the Value value of the Key-Value pair in the returned json object is NULL. +## Examples + +```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/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md index 78ac813a628..26b27cd5aad 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md @@ -24,61 +24,132 @@ specific language governing permissions and limitations under the License. --> -## json_parse -### description -#### Syntax +## Description +Parse the original JSON string into JSON binary format. To meet the needs of different abnormal data processing, different JSON_PARSE series functions are provided as follows: +* JSON_PARSE: Parse the JSON string, and report an error when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_INVALID: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_NULL: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_VALUE: Parse the JSON string, and return the default value specified by the parameter default_json_str when the input string is not a valid JSON string. +* JSON_PARSE_NOTNULL: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +## Alias +* JSONB_PARSE is the same as JSON_PARSE +* JSONB_PARSE_ERROR_TO_INVALID is the same as JSON_PARSE_ERROR_TO_INVALID +* JSONB_PARSE_ERROR_TO_NULL is the same as JSON_PARSE_ERROR_TO_NULL +* JSONB_PARSE_ERROR_TO_VALUE is the same as JSON_PARSE_ERROR_TO_VALUE +* JSONB_PARSE_NOTNULL is the same as JSON_PARSE_NOTNULL + +## Syntax + +```sql +JSON_PARSE (<json_str>) +``` +```sql +JSON_PARSE_ERROR_TO_INVALID (<json_str>) +``` +```sql +JSON_PARSE_ERROR_TO_NULL (<json_str>) +``` + +```sql +JSON_PARSE_ERROR_TO_VALUE (<json_str>, <default_json_str>) +``` ```sql -JSON json_parse(VARCHAR json_str) -JSON json_parse_error_to_null(VARCHAR json_str) -JSON json_parse_error_to_value(VARCHAR json_str, VARCHAR default_json_str) +JSONB_PARSE_NOTNULL (<json_str>) ``` +## Parameters +| Parameter | Description | +|--------------|-----------------------------| +| `<json_str>` | The JSON type parameter or field to be extracted. | +| `<default_json_str>` | When the input string is not a valid JSON string, return the default value specified by the parameter default_json_str. | | + +## Return Values json_parse functions parse JSON string to binary format. A series of functions are provided to satisfy different demand for exception handling. - all return NULL if json_str is NULL - if json_str is not valid - json_parse will report error + - json_parse_error_to_invalid will return NULL - json_parse_error_to_null will return NULL - json_parse_error_to_value will return the value specified by default_json_str + - json_parse_notnull will return NULL -### example - -1. parse valid JSON string - +### Examples +1. Parse valid JSON string +```sql +SELECT json_parse('{"k1":"v31","k2":300}'); ``` -mysql> SELECT json_parse('{"k1":"v31","k2":300}'); +```text +--------------------------------------+ | json_parse('{"k1":"v31","k2":300}') | +--------------------------------------+ | {"k1":"v31","k2":300} | +--------------------------------------+ -1 row in set (0.01 sec) ``` - -2. parse invalid JSON string - +```sql +SELECT json_parse_error_to_invalid('{"k1":"v31","k2":300}'); +``` +```text ++-------------------------------------------------------+ +| jsonb_parse_error_to_invalid('{"k1":"v31","k2":300}') | ++-------------------------------------------------------+ +| {"k1":"v31","k2":300} | ++-------------------------------------------------------+ +``` +```sql +SELECT json_parse_notnull('{"a":"b"}'); ``` -mysql> SELECT json_parse('invalid json'); +```text ++----------------------------------+ +| jsonb_parse_notnull('{"a":"b"}') | ++----------------------------------+ +| {"a":"b"} | ++----------------------------------+ +``` +```sql +SELECT json_parse_error_to_value('{"k1":"v31","k2":300}','{}'); +``` +```text ++-----------------------------------------------------------+ +| jsonb_parse_error_to_value('{"k1":"v31","k2":300}', '{}') | ++-----------------------------------------------------------+ +| {"k1":"v31","k2":300} | ++-----------------------------------------------------------+ +``` +2. Parse invalid JSON string +```sql +SELECT json_parse('invalid json'); +``` +```text ERROR 1105 (HY000): errCode = 2, detailMessage = json parse error: Invalid document: document must be an object or an array for value: invalid json - -mysql> SELECT json_parse_error_to_null('invalid json'); +``` +```sql +SELECT json_parse_error_to_invalid('invalid json'); +``` +```text ++----------------------------------------------+ +| jsonb_parse_error_to_invalid('invalid json') | ++----------------------------------------------+ +| NULL | ++----------------------------------------------+ +``` +```sql +SELECT json_parse_notnull('invalid json'); +``` +```text +-------------------------------------------+ -| json_parse_error_to_null('invalid json') | +| jsonb_parse_error_to_null('invalid json') | +-------------------------------------------+ | NULL | +-------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_parse_error_to_value('invalid json', '{}'); +``` +```sql +SELECT json_parse_error_to_value('invalid json', '{}'); +``` +```text +--------------------------------------------------+ | json_parse_error_to_value('invalid json', '{}') | +--------------------------------------------------+ | {} | +--------------------------------------------------+ -1 row in set (0.00 sec) -``` - -refer to json tutorial for more. - -### keywords -JSONB, JSON, json_parse, json_parse_error_to_null, json_parse_error_to_value \ No newline at end of file +``` \ No newline at end of file diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md index 16a3f35f1a4..1561509dd58 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md @@ -24,47 +24,63 @@ specific language governing permissions and limitations under the License. --> -## json_quote -### Description -#### Syntax +## Description +Enclose json_value in double quotes ("), escape special characters contained. + +## Syntax +```sql +JSON_QUOTE (<a>) +``` -`VARCHAR json_quote(VARCHAR)` +## Parameters +| Parameter | Description | +|-----------|------------------------------------------| +| `<a>` | The value of the json_value to be enclosed. | -Enclose json_value in double quotes ("), escape special characters contained. -### example +## Return Values +Return a json_value. Special cases are as follows: +* If the passed parameter is NULL, return NULL. +### Examples +```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/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md index f019679a759..e60921baae7 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md @@ -24,13 +24,22 @@ specific language governing permissions and limitations under the License. --> -## json_replace +## Description +The JSON_REPLACE function is used to update data in a JSON and return the result. -### Description -#### Syntax +## Syntax +```sql +JSON_REPLACE (<json_str>, <path>, <val>[, <jsonPath>, <val>, ...]) +``` -`VARCHAR json_replace(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## Parameters +| Parameter | Description | +|--------------|---------------------------------------------------------------------------------------------| +| `<json_str>` | The JSON data to be replaced. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON, an error will be returned. | +| `<path>` | The JSON path to be replaced. | +| `<val>` | The value to replace the value corresponding to the JSON_PATH Key. If it is NULL, then a NULL value will be inserted at the corresponding position. | +## Return Values `json_replace` function updates data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -40,30 +49,45 @@ A path-value pair for an existing path in the json overwrites the existing json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +### Examples +```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/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md index 2e0d80cf34f..12dbff80d9a 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md @@ -24,13 +24,24 @@ specific language governing permissions and limitations under the License. --> -## json_set +## Description +The json_set function inserts or updates data in a JSON and returns the result. -### Description -#### Syntax +## Syntax +```sql +JSON_SET (<json_str>, <path>, <val> [, <path>, <val>, ...]) +``` + +## Parameters -`VARCHAR json_set(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +| Parameter | Description | +|-------|-------------------------------------------------------------------------------------------------------------------------| +| `<jsonStr>` | The JSON object to be inserted. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON or any path parameter is not a valid path expression or contains a * wildcard, an error is returned. | +| `<jsonPath>` | The JSON path to be inserted. If it is NULL, then return NULL. | +| `<val>` | The value to be inserted into the JSON. If it is NULL, then a NULL value will be inserted at the corresponding position. | +## Return Values +Returns a JSON value. `json_set` function inserts or updates data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -44,30 +55,35 @@ A path-value pair for an existing path in the json overwrites the existing json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +## Examples +```sql +select json_set(null, null, null); ``` -MySQL> select json_set(null, null, null); +```text +------------------------------+ | json_set(NULL, NULL, 'NULL') | +------------------------------+ | NULL | +------------------------------+ - -MySQL> select json_set('{"k": 1}', "$.k", 2); +``` +```sql +select json_set('{"k": 1}', "$.k", 2); +``` +```text +------------------------------------+ | json_set('{\"k\": 1}', '$.k', '2') | +------------------------------------+ | {"k":2} | +------------------------------------+ - -MySQL> select json_set('{"k": 1}', "$.j", 2); +``` +```sql +select json_set('{"k": 1}', "$.j", 2); +``` +```text +------------------------------------+ | json_set('{\"k\": 1}', '$.j', '2') | +------------------------------------+ | {"k":1,"j":2} | +------------------------------------+ ``` - -### keywords -JSON, json_set diff --git a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md index 4a059b68d79..c7be5654f5e 100644 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md +++ b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md @@ -24,13 +24,24 @@ specific language governing permissions and limitations under the License. --> -## json_unquote -### Description -#### Syntax +## Description +This function unquotes a JSON value and returns the result as a utf8mb4 string. If the argument is NULL, it will return NULL. -`VARCHAR json_unquote(VARCHAR)` +## Syntax +```sql +JSON_UNQUOTE (<a>) +``` +## Parameters +| Parameters | Description | +|------|-------------------------------------------------------| +| `<a>` | The element to be unquoted. | -This function unquotes a JSON value and returns the result as a utf8mb4 string. If the argument is NULL, it will return NULL. +## Return Values + +Returns a utf8mb4 string. Special cases are as follows: +* If the passed parameter is NULL, return NULL. +* If the passed parameter is not a value with double quotes, the value itself will be returned. +* If the passed parameter is not a string, it will be automatically converted to a string and then the value itself will be returned. Escape sequences within a string as shown in the following table will be recognized. Backslashes will be ignored for all other escape sequences. @@ -46,38 +57,76 @@ Escape sequences within a string as shown in the following table will be recogni | \uxxxx | UTF-8 bytes for Unicode value XXXX | +### Examples -### example - +```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/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract.md b/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract.md deleted file mode 100644 index eccf2ad4ea7..00000000000 --- a/versioned_docs/version-2.1/sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "JSON_EXTRACT", - "language": "en" -} ---- - -<!-- -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. ---> diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md index 5a038c24cfe..119fec62a01 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-array.md @@ -24,47 +24,70 @@ specific language governing permissions and limitations under the License. --> -## json_array -### Description -#### Syntax +## Description +Generate a json array containing the specified values, return empty if no values + -`VARCHAR json_array(VARCHAR,...)` +## Syntax +```sql +JSON_ARRAY (<a>, ...) +``` +## Parameters +| Parameter | Description | +|------|---------------------------------------------------------------------------------------------------------------| +| `<a>, ...` | Elements to be included in the JSON array. It can be a single or multiple values of any type, including NULL. | -Generate a json array containing the specified values, return empty if no values -### example +## Return Values +Returns a JSON array containing the specified values. If no values are specified, an empty JSON array is returned. + +## Examples + +```sql +select json_array(); ``` -MySQL> select json_array(); + +```text +--------------+ | json_array() | +--------------+ | [] | +--------------+ +``` -MySQL> select json_array(null); +```sql +select json_array(null); +``` + +```text +--------------------+ | json_array('NULL') | +--------------------+ | [NULL] | +--------------------+ +``` +```sql +SELECT json_array(1, "abc", NULL, TRUE, CURTIME()); +``` - -MySQL> SELECT json_array(1, "abc", NULL, TRUE, CURTIME()); +```text +-----------------------------------------------+ | json_array(1, 'abc', 'NULL', TRUE, curtime()) | +-----------------------------------------------+ | [1, "abc", NULL, TRUE, "10:41:15"] | +-----------------------------------------------+ +``` +```sql +select json_array("a", null, "c"); +``` -MySQL> select json_array("a", null, "c"); +```text +------------------------------+ | json_array('a', 'NULL', 'c') | +------------------------------+ | ["a", NULL, "c"] | +------------------------------+ ``` -### keywords -json,array,json_array diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md index dcea0736f4e..94c97454783 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-exists-path.md @@ -24,23 +24,59 @@ specific language governing permissions and limitations under the License. --> -## json_exists_path - -### description +## Description It is used to judge whether the field specified by json_path exists in the JSON data. If it exists, it returns TRUE, and if it does not exist, it returns FALSE -#### Syntax +## Syntax ```sql -BOOLEAN json_exists_path(JSON j, VARCHAR json_path) +JSON_EXISTS_PATH (<json_str>, <path>) ``` -### example +## Alias + +* JSONB_EXISTS_PATH + +## Parameters +| Parameter | Description | +|--------------|--------------------------------------------------------| +| `<json_str>` | The element to be included in the JSON array. It can be a value of any type, including NULL. If no element is specified, an empty array is returned. +| `<path>` | The JSON path to be judged. If it is NULL, then return NULL. | -Refer to [json tutorial](../../sql-reference/Data-Types/JSON.md) +## Return Values +If it exists, return TRUE; if it does not exist, return FALSE. -### keywords +## Examples -json_exists_path +```sql +SELECT JSON_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.name'); +``` +```text ++---------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.name') | ++---------------------------------------------------------------------------+ +| 1 | ++---------------------------------------------------------------------------+ +``` +```sql +SELECT JSON_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.age'); +``` +```text ++--------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.age') | ++--------------------------------------------------------------------------+ +| 0 | ++--------------------------------------------------------------------------+ +``` +```sql +SELECT JSONB_EXISTS_PATH('{"id": 123, "name": "doris"}', '$.age'); +``` +```text ++--------------------------------------------------------------------------+ +| jsonb_exists_path(cast('{"id": 123, "name": "doris"}' as JSON), '$.age') | ++--------------------------------------------------------------------------+ +| 0 | ++--------------------------------------------------------------------------+ +``` diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md index 39aaf61a8e8..2c29d721558 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-extract.md @@ -24,35 +24,60 @@ specific language governing permissions and limitations under the License. --> -## json_extract - -### description - -#### Syntax - +## Description +JSON_EXTRACT is a series of functions that extract the field specified by json_path from JSON data and provide different series of functions according to the type of the field to be extracted. + +* JSON_EXTRACT returns the VARCHAR type for a json string of the VARCHAR type. +* JSON_EXTRACT_ISNULL returns the BOOLEAN type indicating whether it is a json null. +* JSON_EXTRACT_BOOL returns the BOOLEAN type. +* JSON_EXTRACT_INT returns the INT type. +* JSON_EXTRACT_BIGINT returns the BIGINT type. +* JSON_EXTRACT_LARGEINT returns the LARGEINT type. +* JSON_EXTRACT_DOUBLE returns the DOUBLE type. +* JSON_EXTRACT_STRING returns the STRING type. + +## Alias +* JSONB_EXTRACT is the same as JSON_EXTRACT. +* JSONB_EXTRACT_ISNULL is the same as JSON_EXTRACT_ISNULL. +* JSONB_EXTRACT_BOOL is the same as JSON_EXTRACT_BOOL. +* JSONB_EXTRACT_INT is the same as JSON_EXTRACT_INT. +* JSONB_EXTRACT_BIGINT is the same as JSON_EXTRACT_BIGINT. +* JSONB_EXTRACT_LARGEINT is the same as JSON_EXTRACT_LARGEINT. +* JSONB_EXTRACT_DOUBLE is the same as JSON_EXTRACT_DOUBLE. +* JSONB_EXTRACT_STRING is the same as JSON_EXTRACT_STRING. + +## Syntax ```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>) +``` +Alias functions have the same syntax and usage as the above functions, except for the function names. -json_extract functions extract field specified by json_path from JSON. A series of functions are provided for different datatype. -- json_extract with VARCHAR argument, extract and return VARCHAR datatype -- jsonb_extract extract and return JSON datatype -- json_extract_isnull check if the field is json null and return BOOLEAN datatype -- json_extract_bool extract and return BOOLEAN datatype -- json_extract_int extract and return INT datatype -- json_extract_bigint extract and return BIGINT datatype -- json_extract_largeint extract and return LARGEINT datatype -- json_extract_double extract and return DOUBLE datatype -- json_extract_STRING extract and return STRING datatype - +## Parameters +| Parameter | Description | +|--------------|-----------------------------| +| `<json_str>` | The JSON-type parameter or field to be extracted. | +| `<path>` | The JSON path to extract the target element from the target JSON. | json path syntax: - '$' for json document root - '.k1' for element of json object with key 'k1' @@ -60,73 +85,103 @@ json path syntax: - '[i]' for element of json array at index i - Use '$[last]' to get the last element of json_array, and '$[last-1]' to get the penultimate element, and so on. +## Return Values +According to the type of the field to be extracted, return the data type of the specified JSON_PATH in the target JSON. Special case handling is as follows: +* If the field specified by json_path does not exist in the JSON, return NULL. +* If the actual type of the field specified by json_path in the JSON is inconsistent with the type specified by json_extract_t. +* if it can be losslessly converted to the specified type, return the specified type t; if not, return NULL. -Exception handling is as follows: -- if the field specified by json_path does not exist, return NULL -- if datatype of the field specified by json_path is not the same with type of json_extract_t, return t if it can be cast to t else NULL - - -## json_exists_path and json_type -### description -#### Syntax +## Examples ```sql -BOOLEAN json_exists_path(JSON j, VARCHAR json_path) -STRING json_type(JSON j, VARCHAR json_path) +SELECT json_extract('{"id": 123, "name": "doris"}', '$.id'); ``` -There are two extra functions to check field existence and type -- json_exists_path check the existence of the field specified by json_path, return TRUE or FALS -- json_type get the type as follows of the field specified by json_path, return NULL if it does not exist - - object - - array - - null - - bool - - int - - bigint - - largeint - - double - - string - -### example - -refer to [json tutorial](../../sql-reference/Data-Types/JSON.md) for more. - -``` -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, json_exists_path, json_type \ No newline at end of file +```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/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md index 0b8b8770f08..36fd0aae01a 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-insert.md @@ -24,13 +24,22 @@ specific language governing permissions and limitations under the License. --> -## json_insert -### Description -#### Syntax +## Description +The JSON_INSERT function is used to insert data into JSON and return the result. -`VARCHAR json_insert(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## Syntax +```sql +JSON_INSERT (<json_str>, <path>, <val>[, <path>, <val>, ...]) +``` + +## Parameters +| Parameter | Description | +|-------------|--------------------------------------------------------------------------------------------------------------------------------| +| `<json_str>` | The JSON object to be inserted. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON or any path parameter is not a valid path expression or contains a * wildcard, an error is returned. | +| `<path>` | The JSON path to be inserted. If it is NULL, then return NULL. | +| `<val>` | The value to be inserted into the JSON. If it is NULL, then a NULL value will be inserted at the corresponding position. | `json_insert` function inserts data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -44,30 +53,48 @@ A path-value pair for a nonexisting path in the json adds the value to the json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +## Return Values +Returns a JSON value. + +### Examples +```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/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md index 33199db20a4..23084e66722 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-object.md @@ -24,48 +24,74 @@ specific language governing permissions and limitations under the License. --> -## json_object -### Description -#### Syntax +## Description +Generate a json object containing the specified Key-Value, +an exception error is returned when Key is NULL or the number of parameters are odd. -`VARCHAR json_object(VARCHAR,...)` +## Syntax +```sql +JSON_OBJECT (<key>, <value>[,<key>, <value>, ...]) +``` +## Parameters -Generate a json object containing the specified Key-Value, -an exception error is returned when Key is NULL or the number of parameters are odd. +| Parameter | Description | +|---------|------------------------------------------| +| `<key>` | The Key value in the Key-Value of the generated json object. | +| `<value>` | The Value value in the Key-Value of the generated json object. | | -### example +## Return Values +Return a json object. Special cases are as follows: +* If no parameters are passed, return an empty json object. +* If the number of parameters passed is odd, return an exception error. +* If the passed Key is NULL, return an exception error. +* If the passed Value is NULL, the Value value of the Key-Value pair in the returned json object is NULL. +## Examples + +```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/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md index 78ac813a628..26b27cd5aad 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-parse.md @@ -24,61 +24,132 @@ specific language governing permissions and limitations under the License. --> -## json_parse -### description -#### Syntax +## Description +Parse the original JSON string into JSON binary format. To meet the needs of different abnormal data processing, different JSON_PARSE series functions are provided as follows: +* JSON_PARSE: Parse the JSON string, and report an error when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_INVALID: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_NULL: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +* JSON_PARSE_ERROR_TO_VALUE: Parse the JSON string, and return the default value specified by the parameter default_json_str when the input string is not a valid JSON string. +* JSON_PARSE_NOTNULL: Parse the JSON string, and return NULL when the input string is not a valid JSON string. +## Alias +* JSONB_PARSE is the same as JSON_PARSE +* JSONB_PARSE_ERROR_TO_INVALID is the same as JSON_PARSE_ERROR_TO_INVALID +* JSONB_PARSE_ERROR_TO_NULL is the same as JSON_PARSE_ERROR_TO_NULL +* JSONB_PARSE_ERROR_TO_VALUE is the same as JSON_PARSE_ERROR_TO_VALUE +* JSONB_PARSE_NOTNULL is the same as JSON_PARSE_NOTNULL + +## Syntax + +```sql +JSON_PARSE (<json_str>) +``` +```sql +JSON_PARSE_ERROR_TO_INVALID (<json_str>) +``` +```sql +JSON_PARSE_ERROR_TO_NULL (<json_str>) +``` + +```sql +JSON_PARSE_ERROR_TO_VALUE (<json_str>, <default_json_str>) +``` ```sql -JSON json_parse(VARCHAR json_str) -JSON json_parse_error_to_null(VARCHAR json_str) -JSON json_parse_error_to_value(VARCHAR json_str, VARCHAR default_json_str) +JSONB_PARSE_NOTNULL (<json_str>) ``` +## Parameters +| Parameter | Description | +|--------------|-----------------------------| +| `<json_str>` | The JSON type parameter or field to be extracted. | +| `<default_json_str>` | When the input string is not a valid JSON string, return the default value specified by the parameter default_json_str. | | + +## Return Values json_parse functions parse JSON string to binary format. A series of functions are provided to satisfy different demand for exception handling. - all return NULL if json_str is NULL - if json_str is not valid - json_parse will report error + - json_parse_error_to_invalid will return NULL - json_parse_error_to_null will return NULL - json_parse_error_to_value will return the value specified by default_json_str + - json_parse_notnull will return NULL -### example - -1. parse valid JSON string - +### Examples +1. Parse valid JSON string +```sql +SELECT json_parse('{"k1":"v31","k2":300}'); ``` -mysql> SELECT json_parse('{"k1":"v31","k2":300}'); +```text +--------------------------------------+ | json_parse('{"k1":"v31","k2":300}') | +--------------------------------------+ | {"k1":"v31","k2":300} | +--------------------------------------+ -1 row in set (0.01 sec) ``` - -2. parse invalid JSON string - +```sql +SELECT json_parse_error_to_invalid('{"k1":"v31","k2":300}'); +``` +```text ++-------------------------------------------------------+ +| jsonb_parse_error_to_invalid('{"k1":"v31","k2":300}') | ++-------------------------------------------------------+ +| {"k1":"v31","k2":300} | ++-------------------------------------------------------+ +``` +```sql +SELECT json_parse_notnull('{"a":"b"}'); ``` -mysql> SELECT json_parse('invalid json'); +```text ++----------------------------------+ +| jsonb_parse_notnull('{"a":"b"}') | ++----------------------------------+ +| {"a":"b"} | ++----------------------------------+ +``` +```sql +SELECT json_parse_error_to_value('{"k1":"v31","k2":300}','{}'); +``` +```text ++-----------------------------------------------------------+ +| jsonb_parse_error_to_value('{"k1":"v31","k2":300}', '{}') | ++-----------------------------------------------------------+ +| {"k1":"v31","k2":300} | ++-----------------------------------------------------------+ +``` +2. Parse invalid JSON string +```sql +SELECT json_parse('invalid json'); +``` +```text ERROR 1105 (HY000): errCode = 2, detailMessage = json parse error: Invalid document: document must be an object or an array for value: invalid json - -mysql> SELECT json_parse_error_to_null('invalid json'); +``` +```sql +SELECT json_parse_error_to_invalid('invalid json'); +``` +```text ++----------------------------------------------+ +| jsonb_parse_error_to_invalid('invalid json') | ++----------------------------------------------+ +| NULL | ++----------------------------------------------+ +``` +```sql +SELECT json_parse_notnull('invalid json'); +``` +```text +-------------------------------------------+ -| json_parse_error_to_null('invalid json') | +| jsonb_parse_error_to_null('invalid json') | +-------------------------------------------+ | NULL | +-------------------------------------------+ -1 row in set (0.01 sec) - -mysql> SELECT json_parse_error_to_value('invalid json', '{}'); +``` +```sql +SELECT json_parse_error_to_value('invalid json', '{}'); +``` +```text +--------------------------------------------------+ | json_parse_error_to_value('invalid json', '{}') | +--------------------------------------------------+ | {} | +--------------------------------------------------+ -1 row in set (0.00 sec) -``` - -refer to json tutorial for more. - -### keywords -JSONB, JSON, json_parse, json_parse_error_to_null, json_parse_error_to_value \ No newline at end of file +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md index 16a3f35f1a4..1561509dd58 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-quote.md @@ -24,47 +24,63 @@ specific language governing permissions and limitations under the License. --> -## json_quote -### Description -#### Syntax +## Description +Enclose json_value in double quotes ("), escape special characters contained. + +## Syntax +```sql +JSON_QUOTE (<a>) +``` -`VARCHAR json_quote(VARCHAR)` +## Parameters +| Parameter | Description | +|-----------|------------------------------------------| +| `<a>` | The value of the json_value to be enclosed. | -Enclose json_value in double quotes ("), escape special characters contained. -### example +## Return Values +Return a json_value. Special cases are as follows: +* If the passed parameter is NULL, return NULL. +### Examples +```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/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md index f019679a759..cd0f7d74944 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-replace.md @@ -24,13 +24,22 @@ specific language governing permissions and limitations under the License. --> -## json_replace +## Description +The JSON_REPLACE function is used to update data in a JSON and return the result. -### Description -#### Syntax +## Syntax +```sql +JSON_REPLACE (<json_str>, <path>, <val>[, <jsonPath>, <val>, ...]) +``` -`VARCHAR json_replace(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## Parameters +| Parameter | Description | +|--------------|---------------------------------------------------------------------------------------------| +| `<json_str>` | The JSON data to be replaced. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON, an error will be returned. | +| `<path>` | The JSON path to be replaced. | +| `<val>` | The value to replace the value corresponding to the JSON_PATH Key. If it is NULL, then a NULL value will be inserted at the corresponding position. | +## Return Values `json_replace` function updates data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -40,30 +49,45 @@ A path-value pair for an existing path in the json overwrites the existing json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +### Examples +```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 | ++--------------------------------------+ +``` \ No newline at end of file diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md index 2e0d80cf34f..32618af463d 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-set.md @@ -24,13 +24,26 @@ specific language governing permissions and limitations under the License. --> -## json_set -### Description -#### Syntax -`VARCHAR json_set(VARCHAR json_str, VARCHAR path, VARCHAR val[, VARCHAR path, VARCHAR val] ...)` +## Description +The json_set function inserts or updates data in a JSON and returns the result. +## Syntax +```sql +JSON_SET (<json_str>, <path>, <val> [, <path>, <val>, ...]) +``` + +## Parameters + +| Parameter | Description | +|-------|-------------------------------------------------------------------------------------------------------------------------| +| `<jsonStr>` | The JSON object to be inserted. It can be a JSON object with elements of any type, including NULL. If no elements are specified, an empty array is returned. If json_str is not a valid JSON or any path parameter is not a valid path expression or contains a * wildcard, an error is returned. | +| `<jsonPath>` | The JSON path to be inserted. If it is NULL, then return NULL. | +| `<val>` | The value to be inserted into the JSON. If it is NULL, then a NULL value will be inserted at the corresponding position. | + +## Return Values +Returns a JSON value. `json_set` function inserts or updates data in a JSON and returns the result.Returns NULL if `json_str` or `path` is NULL. Otherwise, an error occurs if the `json_str` argument is not a valid JSON or any path argument is not a valid path expression or contains a * wildcard. @@ -44,30 +57,35 @@ A path-value pair for an existing path in the json overwrites the existing json Otherwise, a path-value pair for a nonexisting path in the json is ignored and has no effect. -### example +## Examples +```sql +select json_set(null, null, null); ``` -MySQL> select json_set(null, null, null); +```text +------------------------------+ | json_set(NULL, NULL, 'NULL') | +------------------------------+ | NULL | +------------------------------+ - -MySQL> select json_set('{"k": 1}', "$.k", 2); +``` +```sql +select json_set('{"k": 1}', "$.k", 2); +``` +```text +------------------------------------+ | json_set('{\"k\": 1}', '$.k', '2') | +------------------------------------+ | {"k":2} | +------------------------------------+ - -MySQL> select json_set('{"k": 1}', "$.j", 2); +``` +```sql +select json_set('{"k": 1}', "$.j", 2); +``` +```text +------------------------------------+ | json_set('{\"k\": 1}', '$.j', '2') | +------------------------------------+ | {"k":1,"j":2} | +------------------------------------+ ``` - -### keywords -JSON, json_set diff --git a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md index 4a059b68d79..c7be5654f5e 100644 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md +++ b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/json-unquote.md @@ -24,13 +24,24 @@ specific language governing permissions and limitations under the License. --> -## json_unquote -### Description -#### Syntax +## Description +This function unquotes a JSON value and returns the result as a utf8mb4 string. If the argument is NULL, it will return NULL. -`VARCHAR json_unquote(VARCHAR)` +## Syntax +```sql +JSON_UNQUOTE (<a>) +``` +## Parameters +| Parameters | Description | +|------|-------------------------------------------------------| +| `<a>` | The element to be unquoted. | -This function unquotes a JSON value and returns the result as a utf8mb4 string. If the argument is NULL, it will return NULL. +## Return Values + +Returns a utf8mb4 string. Special cases are as follows: +* If the passed parameter is NULL, return NULL. +* If the passed parameter is not a value with double quotes, the value itself will be returned. +* If the passed parameter is not a string, it will be automatically converted to a string and then the value itself will be returned. Escape sequences within a string as shown in the following table will be recognized. Backslashes will be ignored for all other escape sequences. @@ -46,38 +57,76 @@ Escape sequences within a string as shown in the following table will be recogni | \uxxxx | UTF-8 bytes for Unicode value XXXX | +### Examples -### example - +```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/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract.md b/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract.md deleted file mode 100644 index eccf2ad4ea7..00000000000 --- a/versioned_docs/version-3.0/sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract.md +++ /dev/null @@ -1,25 +0,0 @@ ---- -{ - "title": "JSON_EXTRACT", - "language": "en" -} ---- - -<!-- -Licensed to the Apache Software Foundation (ASF) under one -or more contributor license agreements. See the NOTICE file -distributed with this work for additional information -regarding copyright ownership. The ASF licenses this file -to you under the Apache License, Version 2.0 (the -"License"); you may not use this file except in compliance -with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, -software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, either express or implied. See the License for the -specific language governing permissions and limitations -under the License. ---> diff --git a/versioned_sidebars/version-2.1-sidebars.json b/versioned_sidebars/version-2.1-sidebars.json index 4e6f6a69f3c..2ac1a0680ea 100644 --- a/versioned_sidebars/version-2.1-sidebars.json +++ b/versioned_sidebars/version-2.1-sidebars.json @@ -1272,7 +1272,6 @@ "sql-manual/sql-functions/scalar-functions/json-functions/json-insert", "sql-manual/sql-functions/scalar-functions/json-functions/json-replace", "sql-manual/sql-functions/scalar-functions/json-functions/json-set", - "sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract", "sql-manual/sql-functions/scalar-functions/json-functions/json-extract-bigint", "sql-manual/sql-functions/scalar-functions/json-functions/json-extract-largeint", "sql-manual/sql-functions/scalar-functions/json-functions/json-extract-bool", diff --git a/versioned_sidebars/version-3.0-sidebars.json b/versioned_sidebars/version-3.0-sidebars.json index 895f16350df..f00b3f7bff0 100644 --- a/versioned_sidebars/version-3.0-sidebars.json +++ b/versioned_sidebars/version-3.0-sidebars.json @@ -1326,7 +1326,6 @@ "sql-manual/sql-functions/scalar-functions/json-functions/json-replace", "sql-manual/sql-functions/scalar-functions/json-functions/json-set", "sql-manual/sql-functions/scalar-functions/json-functions/json-keys", - "sql-manual/sql-functions/scalar-functions/json-functions/jsonb-extract", "sql-manual/sql-functions/scalar-functions/json-functions/json-extract-bigint", "sql-manual/sql-functions/scalar-functions/json-functions/json-extract-largeint", "sql-manual/sql-functions/scalar-functions/json-functions/json-extract-bool", --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org