简体   繁体   中英

How to select field values from array of objects?

I have a JSON column with following JSON

{
    "metadata": { "value": "JABC" },
    "force": false,
    "users": [ 
        { "id": "111", "comment": "abc" },
        { "id": "222", "comment": "abc" },
        { "id": "333" }
    ]
}

I am expecting list of IDs from the query output ["111","222", "333"] . I tried following query but getting null value.

select colName->'users'->>'id' ids from tableName

How to get this specific field value from the array of object?

You need to extract the array as rows and then get the id :

select json_array_elements(colName->'users')->>'id' ids from tableName;

If you're using jsonb rather than json , the function is jsonb_array_elements .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM