Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from postgresql json array field in an array

Below is a json data in a column name meta and table name layer. Here i retrieve data of 'pages' key by meta->'pages' but i have no idea how to get 'lable' key value which is an array element of 'fields' which is again in an array pages.

{
   "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"
            }
         ]
      }
   ]
}
like image 977
Avinash Avatar asked Aug 16 '17 09:08

Avinash


People also ask

How do I query JSON data in PostgreSQL?

Querying the JSON documentPostgreSQL has two native operators -> and ->> to query JSON documents. The first operator -> returns a JSON object, while the operator ->> returns text. These operators work on both JSON as well as JSONB columns. There are additional operators available for JSONB columns.

Can Jsonb be an array?

(Note: It is possible to make a jsonb[] column, but we don't recommend it, as there's no value over a jsonb column that contains an array.)


2 Answers

One way with json_array_elements:

(assuming that your table is your_table and json column name is meta)

select j.value->>'lable'
from your_table
join lateral json_array_elements(meta->'pages'->0->'fields') j
on true
like image 132
Oto Shavadze Avatar answered Nov 15 '22 04:11

Oto Shavadze


You can index an array by number:

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

To retrieve the "lables" of all organisations, create a table with jsonb_to_recordset:

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

The second argument to jsonb_to_recordset defines the columns you are interested in. Here I'm using orgs(name text, lable text) to make the name and lable available.

To get the value of a nested JSON dictionary, you can use a lateral join:

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.

like image 25
Andomar Avatar answered Nov 15 '22 03:11

Andomar