Version sans cache

Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

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. +---------------------+-----------------------+

Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-json-attribute-functions.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut