The functions listed in this section compose JSON values from component elements.本节中列出的函数从组件元素组成JSON值。
Evaluates a (possibly empty) list of values and returns a JSON array containing those values.计算值列表(可能为空),并返回包含这些值的JSON数组。
mysql> SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME());
+---------------------------------------------+
| JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()) |
+---------------------------------------------+
| [1, "abc", null, true, "11:30:24.000000"] |
+---------------------------------------------+
JSON_OBJECT([
key
, val
[, key
, val
] ...])
Evaluates a (possibly empty) list of key-value pairs and returns a JSON object containing those pairs. An error occurs if any key name is NULL
or the number of arguments is odd.
mysql> SELECT JSON_OBJECT('id', 87, 'name', 'carrot');
+-----------------------------------------+
| JSON_OBJECT('id', 87, 'name', 'carrot') |
+-----------------------------------------+
| {"id": 87, "name": "carrot"} |
+-----------------------------------------+
Quotes a string as a JSON value by wrapping it with double quote characters and escaping interior quote and other characters, then returning the result as a 通过使用双引号字符和转义内部引号以及其他字符将字符串括起来作为JSON值引用,然后将结果作为utf8mb4
string. utf8mb4
字符串返回。Returns 如果参数为NULL
if the argument is NULL
.NULL
,则返回NULL
。
This function is typically used to produce a valid JSON string literal for inclusion within a JSON document.此函数通常用于生成一个有效的JSON字符串文本以包含在JSON文档中。
Certain special characters are escaped with backslashes per the escape sequences shown in Table 12.23, “JSON_UNQUOTE() Special Character Escape Sequences”.某些特殊字符按照表12.23,“JSON_UNQUOTE()
特殊字符转义序列”中的转义序列用反斜杠转义。
mysql>SELECT JSON_QUOTE('null'), JSON_QUOTE('"null"');
+--------------------+----------------------+ | JSON_QUOTE('null') | JSON_QUOTE('"null"') | +--------------------+----------------------+ | "null" | "\"null\"" | +--------------------+----------------------+ mysql>SELECT JSON_QUOTE('[1, 2, 3]');
+-------------------------+ | JSON_QUOTE('[1, 2, 3]') | +-------------------------+ | "[1, 2, 3]" | +-------------------------+
You can also obtain JSON values by casting values of other types to the 您还可以通过使用JSON
type using CAST(
; see Converting between JSON and non-JSON values, for more information.value
AS JSON)CAST(
将其他类型的值强制转换为JSON类型来获取JSON值;有关更多信息,请参阅在JSON和非JSON值之间转换。value
AS JSON)
Two aggregate functions generating JSON values are available. 有两个生成JSON值的聚合函数可用。JSON_ARRAYAGG()
returns a result set as a single JSON array, and JSON_OBJECTAGG()
returns a result set as a single JSON object. JSON_ARRAYAGG()
将结果集作为单个JSON数组返回,JSON_OBJECTAGG()
将结果集作为单个JSON对象返回。For more information, see Section 12.20, “Aggregate Functions”.有关更多信息,请参阅第12.20节,“聚合函数”。