sql >> Base de Datos >  >> RDS >> MariaDB

Explicación de MariaDB JSON_DEPTH()

En MariaDB, JSON_DEPTH() es una función integrada que le permite verificar la profundidad de un documento JSON.

Acepta el documento JSON como argumento y devuelve la profundidad máxima del documento.

Sintaxis

La sintaxis es así:

JSON_DEPTH(json_doc)

Donde json_doc es el documento JSON para el que devolver la profundidad.

Ejemplo

Aquí hay un ejemplo para demostrarlo.

SELECT JSON_DEPTH('{ "name": "Wag" }');

Resultado:

+---------------------------------+
| JSON_DEPTH('{ "name": "Wag" }') |
+---------------------------------+
|                               2 |
+---------------------------------+

En este caso, la profundidad es 2 .

Valores escalares y matrices/objetos vacíos

Los valores escalares o matrices u objetos vacíos tienen una profundidad de 1 :

SELECT 
    JSON_DEPTH('{}'),
    JSON_DEPTH('[]'),
    JSON_DEPTH(1);

Resultado:

+------------------+------------------+---------------+
| JSON_DEPTH('{}') | JSON_DEPTH('[]') | JSON_DEPTH(1) |
+------------------+------------------+---------------+
|                1 |                1 |             1 |
+------------------+------------------+---------------+

Documento JSON más profundo

Aquí hay un ejemplo que usa un documento JSON con una profundidad de 4 :

SET @json_document = '
    { 
        "_id" : 1, 
        "name" : "Wag", 
        "details" : {
            "type" : "Dog", 
            "weight" : 20,
            "awards" : { 
                "Florida Dog Awards" : "Top Dog", 
                "New York Marathon" : "Fastest Dog", 
                "Sumo 2020" : "Biggest Dog"
            }
        }
    }
';
SELECT JSON_DEPTH(@json_document);

Resultado:

+----------------------------+
| JSON_DEPTH(@json_document) |
+----------------------------+
|                          4 |
+----------------------------+

Argumentos nulos

Si el argumento es NULL , el resultado es NULL :

SELECT JSON_DEPTH(null);

Resultado:

+------------------+
| JSON_DEPTH(null) |
+------------------+
|             NULL |
+------------------+

JSON no válido

Pasar JSON no válido da como resultado NULL con una advertencia:

SELECT JSON_DEPTH('{1}');

Resultado:

+-------------------+
| JSON_DEPTH('{1}') |
+-------------------+
|              NULL |
+-------------------+
1 row in set, 1 warning (0.000 sec)

Veamos la advertencia:

SHOW WARNINGS;

Resultado:

+---------+------+--------------------------------------------------------------------------------+
| Level   | Code | Message                                                                        |
+---------+------+--------------------------------------------------------------------------------+
| Warning | 4038 | Syntax error in JSON text in argument 1 to function 'json_depth' at position 2 |
+---------+------+--------------------------------------------------------------------------------+

Recuento de parámetros incorrecto

No proporcionar argumentos da como resultado un error:

SELECT JSON_DEPTH();

Resultado:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DEPTH'

Es lo mismo cuando proporciona demasiados argumentos:

SELECT JSON_DEPTH('{"a": 1}', 2);

Resultado:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'JSON_DEPTH'