我在PSQL表中有一个JSONB列,其中包含大型JSON对象(最大700kb)。我想在对象内部获取一个嵌套数组,并将其写入PSQL表。在纯PSQL中,所有这些充其量都是充其量。有办法吗?
{"a": "foo", "b": [{"c": 123}, {"c": 456}]}
// should become
id | data
1 | {"c": 123}
2 | {"c": 456}
答案 0 :(得分:1)
假设您的表的名称为table_name
,而jsonb列的名称为jsonbColumn
,这将达到目的:
with items as (
select jsonb_array_elements(jsonbColumn -> 'b') as item
from table_name)
select ROW_NUMBER() OVER () AS id, * from items
答案 1 :(得分:0)
那很有帮助,非常感谢。只是为了完整起见,请执行以下写操作:
with items as (
select jsonb_array_elements(jsonbColumn -> 'b') as item from table_name
)
insert into another_table (id, content) select item->>'c' as id, item from items