如何在MySQL中从我的JSON中删除空属性
我有一个存储JSON值的表。在这些JSON中,JSON具有如下空属性:如何在MySQL中从我的JSON中删除空属性,mysql,Mysql,我有一个存储JSON值的表。在这些JSON中,JSON具有如下空属性: { "name" : "AAAA", "department" : "BBBB", "countryCode" : null, "languageCode" : null, "region" : "AP" } 我想编写一个查询,以便从输出中删除所有空属性。例如,对于上述JSON,结果输出JSON应该如下所示 { "name" : "AAAA", "department" : "BBBB"
{
"name" : "AAAA",
"department" : "BBBB",
"countryCode" : null,
"languageCode" : null,
"region" : "AP"
}
我想编写一个查询,以便从输出中删除所有空属性。例如,对于上述JSON,结果输出JSON应该如下所示
{
"name" : "AAAA",
"department" : "BBBB",
"region" : "AP"
}
我希望有一个通用查询,可以应用于任何JSON,以消除MySQL v5.7中的null属性。以下查询将用于删除单个键值对,其中值为null: 但是,我看不到一个干净的方法可以在一次更新中进行多次删除。我们可以尝试将更新链接在一起,但这样做既难看又不可读 另外,为了检查JSON null,我必须首先将值转换为文本
如何使用JSON_remove函数删除空键。$。如果条件为false,则使用dummy
select json_remove(abc,
case when json_unquote(abc->'$.name') = 'null' then '$.name' else '$.dummy' end,
case when json_unquote(abc->'$.department') = 'null' then '$.department' else '$.dummy' end,
case when json_unquote(abc->'$.countryCode') = 'null' then '$.countryCode' else '$.dummy' end,
case when json_unquote(abc->'$.languageCode') = 'null' then '$.languageCode' else '$.dummy' end,
case when json_unquote(abc->'$.region') = 'null' then '$.region' else '$.dummy' end)
from (
select cast('{
"name" : "AAAA",
"department" : "BBBB",
"countryCode" : null,
"languageCode" : null,
"region" : "AP"
}' as json) as abc ) a
输出:
{"name": "AAAA", "region": "AP", "department": "BBBB"}
共享您的查询您可以使用mysql的JSON_删除功能。
{"name": "AAAA", "region": "AP", "department": "BBBB"}