2017-08-16 20 views
1

以下は、列名のメタデータとテーブル名のjsonデータです。 ここで私はmeta - > 'pages'で 'pages'キーのデータを取得しますが、 'lable'キー値を 'fields'の配列要素である配列のページに戻す方法はわかりません。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辞書の値を取得するには、横方向の参加を使用することができますjson列名はmeta

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

jsonb_to_recordsetでテーブルを作成し、すべての組織の "lables" を取得するに

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

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)を使用して、名前とlableを使用可能にしています。あなたのテーブルがyour_tableとであると仮定すると(

json_array_elements

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

組織名、説明、組織。タイプ、チェックボックス、アップロード、GIS、日付、添付ファイル – Avinash

+0

ここにあなたのjsonが何を意味しますか? – Avinash

+0

をレイヤーテーブルの選択で置き換えました – Andomar

関連する問題