Supprime tous les champs ou éléments dont la valeur est null d'un objet JSON ou d'un tableau JSON.
Syntaxe
JSON JSON_STRIP_NULLS(
JSON <json_expr>
[, BOOLEAN <include_arrays> ]
[, BOOLEAN <remove_empty> ]
[, STRING <json_path> ]
)
Paramètres
Parameter | Required | Description |
json_expr | Oui | Type JSON. Objet ou tableau JSON. Pour plus d'informations, consultez Type de données JSON. |
include_arrays | Non | Type BOOLEAN. Indique s'il faut supprimer les valeurs null des tableaux JSON. Valeurs possibles :
|
remove_empty | Non | Type BOOLEAN. Indique s'il faut supprimer les objets JSON vides après la suppression des valeurs null. Valeurs possibles :
Remarque Si la valeur de remove_empty est |
json_path | Non | Type STRING. Cette fonction supprime les valeurs null situées au chemin spécifié par json_path dans json_expr. Remarque Si vous utilisez le paramètre json_path, vous devez spécifier les quatre paramètres d'entrée ; json_path doit être le quatrième. |
Valeur renvoyée
Renvoie une valeur de type JSON. Les règles suivantes s'appliquent :
Si un champ d'un objet JSON a une valeur null, la paire clé-valeur associée est supprimée.
Si l'objet ou le tableau JSON résultant est vide, la fonction renvoie une valeur JSON null.
Si json_path est NULL ou non valide, la fonction renvoie json_expr.
Si json_expr, include_arrays ou remove_empty est NULL, la fonction renvoie NULL.
Exemples
-
Exemple 1 : Supprimer les valeurs null d'un objet JSON et d'un tableau JSON.
SELECT JSON_STRIP_NULLS(NULL) AS json_data1, JSON_STRIP_NULLS(JSON 'null') AS json_data2, JSON_STRIP_NULLS(JSON '[1, null, 2, null]') AS json_data3, JSON_STRIP_NULLS(JSON '[1, null, 2, null]', FALSE) AS json_data4, JSON_STRIP_NULLS(JSON '[1, null, 2, null, [null]]',TRUE,TRUE) AS json_data5, JSON_STRIP_NULLS(JSON '[1, null, 2, null, [null]]',NULL,TRUE) AS json_data6, JSON_STRIP_NULLS( JSON '{"a": {"b": {"c": null}}, "d": [null], "e": [], "f": 1}', FALSE,TRUE) AS json_data7, JSON_STRIP_NULLS( JSON '{"a": {"b": {"c": null}}, "d": [null], "e": [], "f": 1}', TRUE,TRUE) AS json_data8;Le résultat suivant est renvoyé :
+------------+------------+------------+-----------------+------------+------------+---------------------------+------------+ | json_data1 | json_data2 | json_data3 | json_data4 | json_data5 | json_data6 | json_data7 | json_data8 | +------------+------------+------------+-----------------+------------+------------+---------------------------+------------+ | NULL | null | [1,2] | [1,null,2,null] | [1,2] | NULL | {"d":[null],"e":[],"f":1} | {"f":1} | +------------+------------+------------+-----------------+------------+------------+---------------------------+------------+ -
Exemple 2 : Supprimer les objets vides à un chemin JSON spécifié.
SELECT JSON_STRIP_NULLS( JSON '{ "person": { "name": "Alice", "address": { "street": null, "city": "New York", "zip": null }, "phone": null }, "company": { "name": "Acme Corp", "location": null } }', true, true, '$.person.address' ) AS json_data;Le résultat suivant est renvoyé :
{"person":{"name":"Alice","phone":null,"address":{"city":"New York"}},"company":{"name":"Acme Corp","location":null}} -
Exemple 3 : Si
json_pathn'est pas valide, la fonction renvoiejson_exprsans effectuer aucune opération.SELECT JSON_STRIP_NULLS( JSON '{ "person": { "name": "Alice", "address": { "street": null, "city": "New York", "zip": null }, "phone": null }, "company": { "name": "Acme Corp", "location": null }, "uid":null }', true, true, '$person.a' ) AS json_data;Le résultat suivant est renvoyé :
{"uid":null,"person":{"name":"Alice","phone":null,"address":{"zip":null,"city":"New York","street":null}},"company":{"name":"Acme Corp","location":null}}
Fonctions connexes
JSON_STRIP_NULLS est une fonction JSON. Pour plus d'informations sur les fonctions JSON, consultez Fonctions JSON.