PostgreSQL:如何对包含数组的json列执行选择查询

时间:2020-04-24 20:09:55

标签: arrays json postgresql

如果我运行以下查询:

select headers from database_name.schema_name.table;

输出是一个像这样的JSON数组:

enter image description here

如何查询PostgreSQL 12仅返回“发件人”节点?

预期结果:

*From*
Me
Him
His

1 个答案:

答案 0 :(得分:1)

您需要取消嵌套数组,然后按索引访问元素:

select x.h ->> 1 as "From"
from t
  cross join jsonb_array_elements(t.headers) as x(h)
where x.h ->> 0 = 'From'  

Online example