PostgreSql遍历数组获取同一列下多行的结果

PostgreSql Traverse through an array to fetch results in multiple rows under same column

我有JSON个数据如下:

"group_nw4qu40":[
{"group_nw4qu40/Special_Characteristics":"11"},
{"group_nw4qu40/Special_Characteristics":"0"},
{"group_nw4qu40/Special_Characteristics":"0"},
{"group_nw4qu40/Special_Characteristics":"1"},
{"group_nw4qu40/Special_Characteristics":"1"}]

我的任务是遍历数组中的数组以获取每个成员的 "Special_Characteristic" 并创建一个 table 在同一列下的多行中具有这些值。

我尝试使用此查询,但它获取了多列中的值,这对我来说是不正确的。

select id AS id, (json->'group_nw4qu40'->>0)::json->>'group_nw4qu40/Special_Characteristics' AS value_of_special_characteristic from public.logger_instance where id = 5215

请帮我在同一列下的多个行中获取这些值。

使用jsonb_array_elements() or json_array_elements(),例如:

with the_data (json) as (
    values (
        '{"group_nw4qu40":[
        {"group_nw4qu40/Special_Characteristics":"11"},
        {"group_nw4qu40/Special_Characteristics":"0"},
        {"group_nw4qu40/Special_Characteristics":"0"},
        {"group_nw4qu40/Special_Characteristics":"1"},
        {"group_nw4qu40/Special_Characteristics":"1"}]}'::jsonb)
    )
select e->>'group_nw4qu40/Special_Characteristics' value_of_special_characteristic
from the_data, jsonb_array_elements(json->'group_nw4qu40') e

 value_of_special_characteristic 
---------------------------------
 11
 0
 0
 1
 1
(5 rows)

您的实际查询应如下所示:

select id, e->>'group_nw4qu40/Special_Characteristics' value_of_special_characteristic
from public.logger_instance, jsonb_array_elements(json->'group_nw4qu40') e 
where id = 5215;