2017-08-16 94 views
1

下面是列名元和表名層中的json數據。 在這裏,我檢索'網頁'鍵的元數據 - >'網頁'的數據,但我不知道如何獲得'lable'鍵值,這是一個數組元素的'字段',這又是一個數組頁面。如何從數組中的postgresql json數組字段中獲取數據

{ 
    "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" 
      } 
     ] 
     } 
    ] 
} 
+0

Postgre文檔是我見過的最好的。 JSON頁面在這裏。 https://www.postgresql.org/docs/current/static/functions-json.html – Mokadillion

+0

是的,我讀了那些,但無法得到它。 – Avinash

+0

我的postgres不接受'TRUE',只好用'「TRUE」或'true'替換它。 – Andomar

回答

1

一種方式與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

你可以索引由數字數組:

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.

+0

想檢索值:Organization Name,Description,Org。類型,複選框,上傳,地理信息系統,日期,附件 – Avinash

+0

你是什麼意思<你的json在這裏> – Avinash

+0

替換<你的json在這裏>在層表上選擇 – Andomar

相關問題