No cache version.


Caching disabled. Default setting for this page:enabled (code DEF204)
If the display is too slow, you can disable the user mode to view the cached version.

Rechercher dans le manuel MySQL

12.17.5 Functions That Return JSON Value Attributes

The functions in this section return attributes of JSON values.

  • JSON_DEPTH(json_doc)

    Returns the maximum depth of a JSON document. Returns NULL if the argument is NULL. 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.

    1. mysql> SELECT JSON_DEPTH('{}'), JSON_DEPTH('[]'), JSON_DEPTH('true');
    2. +------------------+------------------+--------------------+
    3. | JSON_DEPTH('{}') | JSON_DEPTH('[]') | JSON_DEPTH('true') |
    4. +------------------+------------------+--------------------+
    5. |                1 |                1 |                  1 |
    6. +------------------+------------------+--------------------+
    7. mysql> SELECT JSON_DEPTH('[10, 20]'), JSON_DEPTH('[[], {}]');
    8. +------------------------+------------------------+
    9. | JSON_DEPTH('[10, 20]') | JSON_DEPTH('[[], {}]') |
    10. +------------------------+------------------------+
    11. |                      2 |                      2 |
    12. +------------------------+------------------------+
    13. mysql> SELECT JSON_DEPTH('[10, {"a": 20}]');
    14. +-------------------------------+
    15. | JSON_DEPTH('[10, {"a": 20}]') |
    16. +-------------------------------+
    17. |                             3 |
    18. +-------------------------------+
  • JSON_LENGTH(json_doc[, path])

    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. Returns NULL if any argument is NULL or the path argument does not identify a value in the document. An error occurs if the json_doc argument is not a valid JSON document or the path 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.

    1. mysql> SELECT JSON_LENGTH('[1, 2, {"a": 3}]');
    2. +---------------------------------+
    3. | JSON_LENGTH('[1, 2, {"a": 3}]') |
    4. +---------------------------------+
    5. |                               3 |
    6. +---------------------------------+
    7. mysql> SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}');
    8. +-----------------------------------------+
    9. | JSON_LENGTH('{"a": 1, "b": {"c": 30}}') |
    10. +-----------------------------------------+
    11. |                                       2 |
    12. +-----------------------------------------+
    13. mysql> SELECT JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b');
    14. +------------------------------------------------+
    15. | JSON_LENGTH('{"a": 1, "b": {"c": 30}}', '$.b') |
    16. +------------------------------------------------+
    17. |                                              1 |
    18. +------------------------------------------------+
  • JSON_TYPE(json_val)

    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:

    1. mysql> SET @j = '{"a": [10, true]}';
    2. mysql> SELECT JSON_TYPE(@j);
    3. +---------------+
    4. | JSON_TYPE(@j) |
    5. +---------------+
    6. | OBJECT        |
    7. +---------------+
    8. mysql> SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a'));
    9. +------------------------------------+
    10. | JSON_TYPE(JSON_EXTRACT(@j, '$.a')) |
    11. +------------------------------------+
    12. | ARRAY                              |
    13. +------------------------------------+
    14. mysql> SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a[0]'));
    15. +---------------------------------------+
    16. | JSON_TYPE(JSON_EXTRACT(@j, '$.a[0]')) |
    17. +---------------------------------------+
    18. | INTEGER                               |
    19. +---------------------------------------+
    20. mysql> SELECT JSON_TYPE(JSON_EXTRACT(@j, '$.a[1]'));
    21. +---------------------------------------+
    22. | JSON_TYPE(JSON_EXTRACT(@j, '$.a[1]')) |
    23. +---------------------------------------+
    24. | BOOLEAN                               |
    25. +---------------------------------------+

    JSON_TYPE() returns NULL if the argument is NULL:

    1. mysql> SELECT JSON_TYPE(NULL);
    2. +-----------------+
    3. | JSON_TYPE(NULL) |
    4. +-----------------+
    5. | NULL            |
    6. +-----------------+

    An error occurs if the argument is not a valid JSON value:

    1. mysql> SELECT JSON_TYPE(1);
    2. ERROR 3146 (22032): Invalid data type for JSON data in argument 1
    3. 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:

  • JSON_VALID(val)

    Returns 0 or 1 to indicate whether a value is valid JSON. Returns NULL if the argument is NULL.

    1. mysql> SELECT JSON_VALID('{"a": 1}');
    2. +------------------------+
    3. | JSON_VALID('{"a": 1}') |
    4. +------------------------+
    5. |                      1 |
    6. +------------------------+
    7. mysql> SELECT JSON_VALID('hello'), JSON_VALID('"hello"');
    8. +---------------------+-----------------------+
    9. | JSON_VALID('hello') | JSON_VALID('"hello"') |
    10. +---------------------+-----------------------+
    11. |                   0 |                     1 |
    12. +---------------------+-----------------------+

Find a PHP function

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

  1. View the html document Language of the document:en Manuel MySQL : https://dev.mysql.com/

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.

Contents Haut