Sql 在Postgres JSONB字段中仅选择非空键
我有一个带有JSONB列的Postgres9.6表Sql 在Postgres JSONB字段中仅选择非空键,sql,postgresql,jsonb,postgres-9.6,Sql,Postgresql,Jsonb,Postgres 9.6,我有一个带有JSONB列的Postgres9.6表 > SELECT id, data FROM my_table ORDER BY id LIMIT 4; id | data ----+--------------------------------------- 1 | {"a": [1, 7], "b": null, "c": [8]} 2 | {"a": [2, 9], "b": [1], "c": null} 3 | {"a":
> SELECT id, data FROM my_table ORDER BY id LIMIT 4;
id | data
----+---------------------------------------
1 | {"a": [1, 7], "b": null, "c": [8]}
2 | {"a": [2, 9], "b": [1], "c": null}
3 | {"a": [8, 9], "b": null, "c": [3, 4]}
4 | {}
如您所见,一些JSON键具有null
值
我想排除这些-是否有一种简单的方法可以仅选择非null
键值对来生成:
id | data
----+---------------------------------------
1 | {"a": [1, 7], "c": [8]}
2 | {"a": [2, 9], "b": [1]}
3 | {"a": [8, 9], "c": [3, 4]}
4 | {}
谢谢 您可以使用jsonb\u strip\u nulls()
在线示例:
请注意,此函数不会删除数组中的null
值。请共享您编写的没有生成所需输出的代码
select id, jsonb_strip_nulls(data) as data
from my_table;