Remove todos os campos ou elementos com valor null de um objeto JSON ou array JSON.
Sintaxe
JSON JSON_STRIP_NULLS(
JSON <json_expr>
[, BOOLEAN <include_arrays> ]
[, BOOLEAN <remove_empty> ]
[, STRING <json_path> ]
)
Parâmetros
Parâmetro | Obrigatório | Descrição |
json_expr | Sim | Tipo JSON. Objeto ou array JSON. Para mais informações, consulte JSON data type. |
include_arrays | Não | Tipo BOOLEAN. Defina se valores null do JSON devem ser excluídos de arrays JSON. Valores válidos:
|
remove_empty | Não | Tipo BOOLEAN. Defina se objetos JSON vazios devem ser removidos após a exclusão dos valores null. Valores válidos:
Nota Se remove_empty for |
json_path | Não | Tipo STRING. Remove valores null do JSON no caminho especificado por json_path em json_expr. Nota Ao usar o parâmetro json_path, especifique quatro parâmetros de entrada; json_path deve ser o quarto parâmetro. |
Valor retornado
Retorna um valor do tipo JSON. Regras aplicáveis:
Se um campo em um objeto JSON tiver valor null, o par chave-valor correspondente será removido.
Se o objeto ou array JSON resultante estiver vazio, a função retorna null JSON.
Se json_path for NULL ou inválido, a função retorna json_expr.
Se json_expr, include_arrays ou remove_empty for NULL, o retorno será NULL.
Exemplos
-
Exemplo 1: Remover valores null de um objeto JSON e de um array 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;Resultado retornado:
+------------+------------+------------+-----------------+------------+------------+---------------------------+------------+ | 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} | +------------+------------+------------+-----------------+------------+------------+---------------------------+------------+ -
Exemplo 2: Remover objetos vazios em um caminho JSON específico.
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;Resultado retornado:
{"person":{"name":"Alice","phone":null,"address":{"city":"New York"}},"company":{"name":"Acme Corp","location":null}} -
Exemplo 3: Se
json_pathfor inválido, a função retornajson_exprsem executar nenhuma operação.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;Resultado retornado:
{"uid":null,"person":{"name":"Alice","phone":null,"address":{"zip":null,"city":"New York","street":null}},"company":{"name":"Acme Corp","location":null}}
Funções relacionadas
JSON_STRIP_NULLS é uma função JSON. Para mais informações sobre funções JSON, consulte JSON functions.