将json数组中特定标记的值放入plain数组中

时间:2014-09-01 21:21:14

标签: sql arrays json postgresql

我在text[]{1,32}返回plv8,我必须比较下面json的内容:

'{"title":"Televisão em cores"
  , "answers":[{"weight":0, "weight2":1, "title":"Não tem"}
             , {"weight":1, "weight2":2, "title":"1"}
             , {"weight":2, "weight2":4, "title":"2"}
             , {"weight":3, "weight2":8, "title":"3"}
             , {"weight":4, "weight2":16, "title":"4 ou +"}]}'

如何获取所有名称weight2并将其转换为text[]?喜欢{1,2,4,8,16}

1 个答案:

答案 0 :(得分:1)

对于Postgres 9.3中的单个jsonj

SELECT ARRAY(SELECT json_array_elements(j->'answers')->>'weight2');

结果:

array
{1,2,4,8,16}

The manual has more.