postgresql将jsonb转换为记录集

时间:2017-03-14 22:30:22

标签: postgresql jsonb

将jsonb转换为所需的表类型存在问题,我们将不胜感激。

我的jsonb是:

{"left": ["ICMB", "IPERIOD", "IADDR", "KV"]}

我想要将其转换为:

position   element
left       ICMB
left       IPERIOD
left       IADDR
left       KV

提前致谢!

1 个答案:

答案 0 :(得分:0)

使用jsonb_each() and jsonb_array_elements_text():

with my_data(j) as (
values
('{"left": ["ICMB", "IPERIOD", "IADDR", "KV"]}'::jsonb)
)

select key as position, element
from my_data,
lateral jsonb_each(j) as each(key, val),
lateral jsonb_array_elements_text(j->key) as element;

 position | element 
----------+---------
 left     | ICMB
 left     | IPERIOD
 left     | IADDR
 left     | KV
(4 rows)