The functions in this section return attributes of JSON values.本节中的函数返回JSON值的属性。
Returns the maximum depth of a JSON document. 返回JSON文档的最大深度。Returns 如果参数为NULL
if the argument is NULL
. NULL
,则返回NULL
。An error occurs if the argument is not a valid JSON document.如果参数不是有效的JSON文档,则会发生错误。
An empty array, empty object, or scalar value has depth 1. 空数组、空对象或标量值的深度为1。A nonempty array containing only elements of depth 1 or nonempty object containing only member values of depth 1 has depth 2. 仅包含深度1元素的非空数组或仅包含深度1成员值的非空对象具有深度2。Otherwise, a JSON document has depth greater than 2.否则,JSON文档的深度大于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 | +-------------------------------+
Returns the length of a JSON document, or, if a 返回JSON文档的长度,或者,如果给定了path
argument is given, the length of the value within the document identified by the path. path
参数,则返回由路径标识的文档中值的长度。Returns 如果任何参数为NULL
if any argument is NULL
or the path
argument does not identify a value in the document. NULL
或path
参数未标识文档中的值,则返回NULL
。An error occurs if the 如果json_doc
argument is not a valid JSON document or the path
argument is not a valid path expression. json_doc
参数不是有效的JSON文档或path
参数不是有效的路径表达式,则会发生错误。Prior to MySQL 8.0.26, an error is also raised if the path expression contains a 在MySQL 8.0.26之前,如果路径表达式包含*
or **
wildcard.*
或**
通配符,也会引发错误。
The length of a document is determined as follows:文件的长度确定如下:
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 | +------------------------------------------------+
Returns a 返回指示JSON值类型的utf8mb4
string indicating the type of a JSON value. utf8mb4
字符串。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()
returns NULL
if the argument is NULL
:NULL
,则JSON_TYPE()
返回NULL
:
mysql> SELECT JSON_TYPE(NULL);
+-----------------+
| JSON_TYPE(NULL) |
+-----------------+
| NULL |
+-----------------+
An error occurs if the argument is not a valid JSON value:如果参数不是有效的JSON值,则会发生错误:
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.
For a non-对于非NULL
, non-error result, the following list describes the possible JSON_TYPE()
return values:NULL
、非错误的结果,以下列表描述了可能的JSON_TYPE()
返回值:
Purely JSON types:纯JSON类型:
OBJECT
: JSON objects:JSON对象
ARRAY
: JSON arrays:JSON数组
BOOLEAN
: The JSON true and false literals:JSON true
和false
文字
NULL
: The JSON null literal:JSONnull
文字
Numeric types:数字类型:
Temporal types:时态类型:
String types:字符串类型:
Binary types:二进制类型:
All other types:所有其他类型:
OPAQUE
(raw bits)
Returns 0 or 1 to indicate whether a value is valid JSON. 返回0或1以指示值是否为有效的JSON。Returns 如果参数为NULL
if the argument is NULL
.NULL
,则返回NULL
。
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 | +---------------------+-----------------------+