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

Explicación de MariaDB JSON_ARRAY_APPEND()

En MariaDB, JSON_ARRAY_APPEND() es una función integrada que agrega valores al final de la matriz o matrices especificadas dentro de un documento JSON y devuelve el resultado.

Sintaxis

La sintaxis es así:

JSON_ARRAY_APPEND(json_doc, path, value[, path, value] ...)

Donde json_doc es el documento JSON, path es la ruta a la que desea agregar el/los valor/es, y value es el valor a agregar.

Ejemplo

Aquí hay un ejemplo para demostrar la función.

SET @json_doc = '[0, 1, 2, 3]';

SELECT JSON_ARRAY_APPEND(@json_doc, '$', 4);

Resultado:

+--------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$', 4) |
+--------------------------------------+
| [0, 1, 2, 3, 4]                      |
+--------------------------------------+

En este caso, el valor 4 se agregó al final de la matriz.

Agregar valores múltiples

Puede agregar múltiples valores dentro de una sola llamada a JSON_ARRAY_APPEND() .

Ejemplo:

SET @json_doc = '[0, 1, 2, 3]';

SELECT JSON_ARRAY_APPEND(@json_doc, '$', 4, '$', 5);

Resultado:

+----------------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$', 4, '$', 5) |
+----------------------------------------------+
| [0, 1, 2, 3, 4, 5]                           |
+----------------------------------------------+

Matrices Múltiples

Puede agregar valores a más de una matriz dentro de la misma llamada a JSON_ARRAY_APPEND() .

Ejemplo:

SET @json_doc = '{"a": [0, 1], "b": [2, 3]}';

SELECT JSON_ARRAY_APPEND(@json_doc, '$.a', 4, '$.b', 5);

Resultado:

+--------------------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$.a', 4, '$.b', 5) |
+--------------------------------------------------+
| {"a": [0, 1, 4], "b": [2, 3, 5]}                 |
+--------------------------------------------------+

Matrices anidadas

Aquí hay un ejemplo de agregar un valor a una matriz anidada:

SET @json_doc = '[0, 1, [2, 3]]';

SELECT JSON_ARRAY_APPEND(@json_doc, '$[2]', 4);

Resultado:

+-----------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$[2]', 4) |
+-----------------------------------------+
| [0, 1, [2, 3, 4]]                       |
+-----------------------------------------+

Y en el siguiente ejemplo, el documento JSON original no contiene una matriz anidada, pero JSON_ARRAY_APPEND() crea una matriz anidada basada en nuestra ruta:

SET @json_doc = '[0, 1, 2, 3]';

SELECT JSON_ARRAY_APPEND(@json_doc, '$[3]', 4);

Resultado:

+-----------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$[3]', 4) |
+-----------------------------------------+
| [0, 1, 2, [3, 4]]                       |
+-----------------------------------------+

Documento JSON más grande

Aquí hay un ejemplo con un documento JSON un poco más grande.

También uso JSON_DETAILED() para embellecer el resultado:

SET @json_doc = '{  
    "pet": {    
       "name": "Fluffy", 
       "diet": ["Fish", "Chicken"]  
    }
 }';
SELECT JSON_DETAILED(
        JSON_ARRAY_APPEND(
            @json_doc, 
            '$.pet.diet', 
            'Water')
);

Resultado:

{
    "pet": 
    {
        "name": "Fluffy",
        "diet": 
        [
            "Fish",
            "Chicken",
            "Water"
        ]
    }
}

Y aquí hay uno que crea una matriz anidada:

SET @json_doc = '{  
    "pet": {    
       "name": "Scratch", 
       "diet": ["Beef", "Water"]
    }
 }';
SELECT JSON_DETAILED(
        JSON_ARRAY_APPEND(
            @json_doc, 
            '$.pet.diet[1]', 
            'Beer')
);

Resultado:

{
    "pet": 
    {
        "name": "Scratch",
        "diet": 
        [
            "Beef",
            
            [
                "Water",
                "Beer"
            ]
        ]
    }
}

Argumentos nulos

Si el primer argumento es NULL , el resultado es NULL :

SELECT JSON_ARRAY_APPEND(null, '$', 4);

Resultado:

+---------------------------------+
| JSON_ARRAY_APPEND(null, '$', 4) |
+---------------------------------+
| NULL                            |
+---------------------------------+

Lo mismo se aplica a la path argumento:

SET @json_doc = '[0, 1, 2, 3]';

SELECT JSON_ARRAY_APPEND(@json_doc, null, 4);

Resultado:

+---------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, null, 4) |
+---------------------------------------+
| NULL                                  |
+---------------------------------------+

Sin embargo, si el value el argumento es NULL , luego NULL se añade a la matriz:

SET @json_doc = '[0, 1, 2, 3]';

SELECT JSON_ARRAY_APPEND(@json_doc, '$', null);

Resultado:

+-----------------------------------------+
| JSON_ARRAY_APPEND(@json_doc, '$', null) |
+-----------------------------------------+
| [0, 1, 2, 3, null]                      |
+-----------------------------------------+

También puede usar JSON_ARRAY_INSERT() para insertar valores en una matriz.