Rechercher dans le manuel MySQL
12.17.5 Functions That Return JSON Value Attributes
The functions in this section return attributes of JSON values.
Returns the maximum depth of a JSON document. Returns
NULL
if the argument isNULL
. An error occurs if the argument is not a valid JSON document.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.
- +------------------+------------------+--------------------+
- | JSON_DEPTH('{}') | JSON_DEPTH('[]') | JSON_DEPTH('true') |
- +------------------+------------------+--------------------+
- | 1 | 1 | 1 |
- +------------------+------------------+--------------------+
- +------------------------+------------------------+
- | JSON_DEPTH('[10, 20]') | JSON_DEPTH('[[], {}]') |
- +------------------------+------------------------+
- | 2 | 2 |
- +------------------------+------------------------+
- +-------------------------------+
- | JSON_DEPTH('[10, {"a": 20}]') |
- +-------------------------------+
- | 3 |
- +-------------------------------+
Returns the length of a JSON document, or, if a
path
argument is given, the length of the value within the document identified by the path. ReturnsNULL
if any argument isNULL
or thepath
argument does not identify a value in the document. An error occurs if thejson_doc
argument is not a valid JSON document or thepath
argument is not a valid path expression or contains a*
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.
- +---------------------------------+
- | JSON_LENGTH('[1, 2, {"a": 3}]') |
- +---------------------------------+
- | 3 |
- +---------------------------------+
- +-----------------------------------------+
- | JSON_LENGTH('{"a": 1, "b": {"c": 30}}') |
- +-----------------------------------------+
- | 2 |
- +-----------------------------------------+
- +------------------------------------------------+
- | JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b') |
- +------------------------------------------------+
- | 1 |
- +------------------------------------------------+
Returns a
utf8mb4
string indicating the type of a JSON value. This can be an object, an array, or a scalar type, as shown here:- +---------------+
- | JSON_TYPE(@j) |
- +---------------+
- | OBJECT |
- +---------------+
- +------------------------------------+
- | JSON_TYPE(JSON_EXTRACT(@j, '$.a')) |
- +------------------------------------+
- | ARRAY |
- +------------------------------------+
- +---------------------------------------+
- | JSON_TYPE(JSON_EXTRACT(@j, '$.a[0]')) |
- +---------------------------------------+
- +---------------------------------------+
- +---------------------------------------+
- | JSON_TYPE(JSON_EXTRACT(@j, '$.a[1]')) |
- +---------------------------------------+
- +---------------------------------------+
JSON_TYPE()
returnsNULL
if the argument isNULL
:- +-----------------+
- +-----------------+
- +-----------------+
An error occurs if the argument is not a valid JSON value:
For a non-
NULL
, non-error result, the following list describes the possibleJSON_TYPE()
return values:Purely JSON types:
OBJECT
: JSON objectsARRAY
: JSON arraysBOOLEAN
: The JSON true and false literalsNULL
: The JSON null literal
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. Returns
NULL
if the argument isNULL
.- +------------------------+
- | JSON_VALID('{"a": 1}') |
- +------------------------+
- | 1 |
- +------------------------+
- +---------------------+-----------------------+
- | JSON_VALID('hello') | JSON_VALID('"hello"') |
- +---------------------+-----------------------+
- | 0 | 1 |
- +---------------------+-----------------------+
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-json-attribute-functions.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.