2017-01-19 4 views
1

からフィールドを抽出する:PostgreSQLの難易列の例jsonbデータ下記のPostgreSQLの9.5.5 を使用してjsonb列

{ 
"item_id": "123456", 
"action_information_1": [ {"value": "259", "action_type": "read"} ], 
"action_information_2": [ {"value": "93", "action_type": "read"} ], 
"action_information_3": [ {"value": "53", "action_type": "read"} ], 
"action_information_4": [ {"value": "35", "action_type": "read"} ] 
} 

私は希望「action_information_1」から「価値」を抽出するプログラムで困難を抱えています259

も上記の「[ 『の前に』 "」、先行している、構文は、私が見てきた他の例とは若干異なるようです。

すべてのヘルプは高く評価され、あなたに感謝

+1

をあなたの例は次のとおりです。また、これは使用して少し短く書くことができ、キーvalue

無効なJSONドキュメントです。キーと値の周りに '' 'を使用する必要があります。' ''ではなく配列 '[...]'を二重引用符で囲む必要がありません。 –

+0

http://www.json.org/ –

+0

配列インデックスが 'jsonb_extract_path(col - > 'action_information_1'、?)に設定されていなければならない場合は、' col - > 'action_information_1' - > 0 - >> 'value''を入力してください。 – cske

答えて

2

あなたはJSON文書内の構文エラー、次の作品を修正した場合:

with test_data (doc) as (
    values (
    '{ 
    "item_id": "123456", 
    "action_information_1": [{"value": "259", "action_type": "read"}], 
    "action_information_2": [{"value": "93", "action_type": "read"}], 
    "action_information_3": [{"value": "53", "action_type": "read"}], 
    "action_information_4": [{"value": "35", "action_type": "read"}] 
    }'::json 
) 
) 
select doc -> 'action_information_1' -> 0 ->> 'value' 
from test_data 

doc -> 'action_information_1'をそのキーの配列、-> 0戻って最初の配列要素と->> 'value'が、その後に関連付けられている値を取得します

select doC#> '{action_information_1,0}' ->> 'value' 
from test_data 
関連する問題