本节中的函数返回 JSON 值的属性。The functions in this section return attributes of JSON values.
返回 JSON 文档的最大深度。如果参数为
NULL,则返回NULL。如果参数不是有效的 JSON 文档,则会发生错误。Returns the maximum depth of a JSON document. ReturnsNULLif the argument isNULL. An error occurs if the argument is not a valid JSON document.一个空数组、空对象或标量值的深度为 1。一个只包含深度为 1 元素的非空数组,或一个只包含深度为 1 成员值的非空对象的深度为 2。否则,JSON 文档的深度大于 2。An empty array, empty object, or scalar value has depth 1. A nonempty array containing only elements of depth 1 or nonempty object containing only member values of depth 1 has depth 2. Otherwise, a JSON document has depth greater than 2.
mysql>
SELECT JSON_DEPTH('{}'), JSON_DEPTH('[]'), JSON_DEPTH('true');+------------------+------------------+--------------------+ | JSON_DEPTH('{}') | JSON_DEPTH('[]') | JSON_DEPTH('true') | +------------------+------------------+--------------------+ | 1 | 1 | 1 | +------------------+------------------+--------------------+ mysql>SELECT JSON_DEPTH('[10, 20]'), JSON_DEPTH('[[], {}]');+------------------------+------------------------+ | JSON_DEPTH('[10, 20]') | JSON_DEPTH('[[], {}]') | +------------------------+------------------------+ | 2 | 2 | +------------------------+------------------------+ mysql>SELECT JSON_DEPTH('[10, {"a": 20}]');+-------------------------------+ | JSON_DEPTH('[10, {"a": 20}]') | +-------------------------------+ | 3 | +-------------------------------+返回 JSON 文档的长度,或者如果提供了
path参数,则返回文档中由该路径标识的值的长度。如果任何参数为NULL,或者path参数未在文档中标识值,则返回NULL。如果json_doc参数不是有效的 JSON 文档,或者path参数不是有效的路径表达式,则会发生错误。Returns the length of a JSON document, or, if apathargument is given, the length of the value within the document identified by the path. ReturnsNULLif any argument isNULLor thepathargument does not identify a value in the document. An error occurs if thejson_docargument is not a valid JSON document or thepathargument is not a valid path expression.文档的长度定义如下:The length of a document is determined as follows:
标量的长度为 1。The length of a scalar is 1.
数组的长度为数组元素的数量。The length of an array is the number of array elements.
对象的长度为对象成员的数量。The length of an object is the number of object members.
长度不包括嵌套数组或对象的长度。The length does not count the length of nested arrays or objects.
mysql>
SELECT JSON_LENGTH('[1, 2, {"a": 3}]');+---------------------------------+ | JSON_LENGTH('[1, 2, {"a": 3}]') | +---------------------------------+ | 3 | +---------------------------------+ mysql>SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}');+-----------------------------------------+ | JSON_LENGTH('{"a": 1, "b": {"c": 30}}') | +-----------------------------------------+ | 2 | +-----------------------------------------+ mysql>SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b');+------------------------------------------------+ | JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b') | +------------------------------------------------+ | 1 | +------------------------------------------------+返回一个
utf8mb4字符串,指示 JSON 值的类型。这个类型可以是对象、数组,或者标量类型,如下所示:Returns autf8mb4string indicating the type of a JSON value. This can be an object, an array, or a scalar type, as shown here:mysql>
SET @j = '{"a": [10, true]}';mysql>SELECT JSON_TYPE(@j);+---------------+ | JSON_TYPE(@j) | +---------------+ | OBJECT | +---------------+ mysql>SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a'));+------------------------------------+ | JSON_TYPE(JSON_EXTRACT(@j, '$.a')) | +------------------------------------+ | ARRAY | +------------------------------------+ mysql>SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a[0]'));+---------------------------------------+ | JSON_TYPE(JSON_EXTRACT(@j, '$.a[0]')) | +---------------------------------------+ | INTEGER | +---------------------------------------+ mysql>SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a[1]'));+---------------------------------------+ | JSON_TYPE(JSON_EXTRACT(@j, '$.a[1]')) | +---------------------------------------+ | BOOLEAN | +---------------------------------------+JSON_TYPE()如果参数为NULL,则返回NULL:JSON_TYPE()returnsNULLif the argument isNULL:mysql>
SELECT JSON_TYPE(NULL);+-----------------+ | JSON_TYPE(NULL) | +-----------------+ | NULL | +-----------------+如果参数不是有效的 JSON 值,将发生错误:An error occurs if the argument is not a valid JSON value:
mysql>
SELECT JSON_TYPE(1);ERROR 3146 (22032): Invalid data type for JSON data in argument 1 to function json_type; a JSON string or JSON type is required.对于非
NULL、非错误结果,以下列表描述了可能的JSON_TYPE()返回值:For a non-NULL, non-error result, the following list describes the possibleJSON_TYPE()return values:纯 JSON 类型:Purely JSON types:
OBJECT:JSON 对象OBJECT: JSON objectsARRAY:JSON 数组ARRAY: JSON arraysBOOLEAN:JSON 的 true 和 false 字面量BOOLEAN: The JSON true and false literalsNULL:JSON 的 null 字面量NULL: The JSON null literal
数值类型:Numeric types:
时间类型:Temporal types:
字符串类型:String types:
二进制类型:Binary types:
所有其他类型:All other types:
OPAQUE(原始位)OPAQUE(raw bits)
返回 0 或 1 以指示一个值是否是有效的 JSON。如果参数为
NULL,则返回NULL。Returns 0 or 1 to indicate whether a value is valid JSON. ReturnsNULLif the argument isNULL.mysql>
SELECT JSON_VALID('{"a": 1}');+------------------------+ | JSON_VALID('{"a": 1}') | +------------------------+ | 1 | +------------------------+ mysql>SELECT JSON_VALID('hello'), JSON_VALID('"hello"');+---------------------+-----------------------+ | JSON_VALID('hello') | JSON_VALID('"hello"') | +---------------------+-----------------------+ | 0 | 1 | +---------------------+-----------------------+