[英]Postgres index creation for jsonb column having JSON Array value
[英]extract jsonb value from json containing json array in postgres
我有 json 列,就像這樣的數據 -
{"image_pose_array": [{"image_name": "0026568143_WS.jpg", "image_pose": "EXTRA", "is_blurred": false, "is_dark": false}], "policy_number": "\"D000000000\""}
我如何生成如下所示的報告 -
image_name | image_pose |
-----------------------------------
0026568143_WS.jpg | EXTRA |
我試過
SELECT response #>>'{image_pose_array,0}' as json
FROM abcd_abcd.table_data
where policy_number ='D017447997'
order by request_id asc;
如何從中提取鍵的值?
您可以使用#>
從數組中訪問整個 object,然后訪問每個鍵:
SELECT response #> '{image_pose_array,0}' ->> 'image_name' as image_name,
response #> '{image_pose_array,0}' ->> 'image_pose' as image_pose
from abcd_abcd.table_data
where policy_number ='D017447997'
order by request_id asc;
#>
和#>>
(您使用的)之間的區別在於, #>
再次返回一個jsonb
值,然后可以進一步訪問該值。 而#>>
返回一個text
值。
或者,您可以在“路徑”參數中再使用#>>
一步:
response #>> '{image_pose_array,0,image_name}' as image_name,
response #>> '{image_pose_array,0,image_pose}' as image_pose
我會使用json[b]_array_elements()
。 這適用於數組包含多個 object 的情況(在這種情況下,查詢將為每個嵌入對象生成一行):
select
t.request_id,
x.obj ->> 'image_name' as image_name,
x.obj ->> 'image_pose' as image_pose
from digit_bots.t_fourwheeler_analysis_data t
cross join lateral jsonb_array_elements(t.response -> 'image_pose_array') as x(obj)
where policy_number ='D017447997'
order by request_id desc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.