【问题标题】:Put values from specific tag in a json array into plain array将 json 数组中特定标签的值放入普通数组中
【发布时间】:2014-09-01 21:21:13
【问题描述】:

我的plv8 中有text[] 返回{1,32},我必须比较下面的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}

【问题讨论】:

    标签: sql arrays json postgresql


    【解决方案1】:

    对于 Postgres 9.3 中的单个 jsonj

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

    结果:

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

    The manual has more.

    【讨论】:

    • 如果 OP 想要一个 text[] 而不是 json[] 数组,那么只需将 ->'weight2' 更改为 ->>'weight2'
    • @ClodoaldoNeto:谢谢,->> 更适合这个问题。
    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2012-07-14
    相关资源
    最近更新 更多