如何从数组中的postgresql json数组字段获取数据

时间:2017-08-16 09:23:53

标签: json postgresql

下面是列名元和表名称层中的json数据。 在这里,我检索'页面的数据'通过meta->'页面'但我不知道如何获得“#lable”'键值是'字段的数组元素'这又是一个数组页面。

{
   "id":1,
   "name":"org_details",
   "action":"organisation.php",
   "lable":"Manage Organisation",
   "pages":[
      {
         "name":"Create Org",
         "lable":"Organisation Name",
         "fields":[
            {
               "id":11,
               "type":1,
               "subtype":1,
               "lable":"Organisation Name"
            },
            {
               "id":12,
               "type":2,
               "subtype":1,
               "lable":"Description",
               "mandatory":TRUE,
               "validations":{
                  "minl":2,
                  "maxl":60
               }
            },
            {
               "id":13,
               "type":3,
               "subtype":1,
               "lable":"Org. Type",
               "default value":1,
               "mandatory":TRUE,
               "choices":[
                  {
                     "lable":"OFSDP",
                     "value":1
                  },
                  {
                     "lable":"AGRICULTURE",
                     "value":2
                  },
                  {
                     "lable":"HUTICULTURE",
                     "value":3
                  }
               ]
            },
            {
               "id":14,
               "type":4,
               "lable":"checkbox",
               "default value":1
            },
            {
               "id":15,
               "type":5,
               "subtype":1,
               "lable":"Upload",
               "mandatory":TRUE
            },
            {
               "id":16,
               "type":6,
               "subtype":1,
               "lable":"GIS"
            },
            {
               "id":17,
               "type":7,
               "subtype":1,
               "lable":"Date"
            },
            {
               "id":18,
               "type":8,
               "lable":"Attachment"
            }
         ]
      }
   ]
}

3 个答案:

答案 0 :(得分:2)

json_array_elements的一种方式:

(假设您的表格为your_table且json列名称为meta

select j.value->>'lable'
from your_table
join lateral json_array_elements(meta->'pages'->0->'fields') j
on true

答案 1 :(得分:1)

您可以按编号索引数组:

select meta::jsonb->'pages'->0->'lable'
from   layer

要检索所有组织的“标签”,请创建一个包含jsonb_to_recordset的表格:

select  orgs.lable
from    layer
cross join
        jsonb_to_recordset(meta::jsonb->'pages') orgs(name text, lable text)

jsonb_to_recordset的第二个参数定义了您感兴趣的列。这里我使用orgs(name text, lable text)来创建名称和标签。

要获取嵌套JSON字典的值,可以使用横向连接:

select  orgs.lable
,       fields.lable
from    layer
cross join
        jsonb_to_recordset(meta::jsonb->'pages')
            orgs(lable text, fields jsonb)
cross join
        jsonb_to_recordset(fields) fields(lable text)

Working example at regtester.

答案 2 :(得分:0)

select '[
{
            "item_type"       : "FEE"
            ,"item_id": 18
            , "item_status"     : "PENDING"
            , "description"          : "auto_pay"
}
]'::json->0->>'item_id' as item_id;

 item_id 
--------------
 18