![](/img/trans.png)
[英]Select an array with all the values of a particular field from an array of objects in Postgres
[英]How to select field values from array of objects?
我有一個帶有以下JSON的JSON列
{
"metadata": { "value": "JABC" },
"force": false,
"users": [
{ "id": "111", "comment": "abc" },
{ "id": "222", "comment": "abc" },
{ "id": "333" }
]
}
我期望查詢輸出["111","222", "333"]
的ID列表。 我試過以下查詢,但得到空值。
select colName->'users'->>'id' ids from tableName
如何從對象數組中獲取此特定字段值?
您需要將數組提取為行,然后獲取id
:
select json_array_elements(colName->'users')->>'id' ids from tableName;
如果您使用的是jsonb
而不是json
,則此函數為jsonb_array_elements
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.