简体   繁体   English

如何从对象数组中选择字段值?

[英]How to select field values from array of objects?

I have a JSON column with following JSON 我有一个带有以下JSON的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"] . 我期望查询输出["111","222", "333"]的ID列表。 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 : 您需要将数组提取为行,然后获取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 . 如果您使用的是jsonb而不是json ,则此函数为jsonb_array_elements

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Postgres中的对象数组中选择一个具有特定字段所有值的数组 - Select an array with all the values of a particular field from an array of objects in Postgres 如何从 json 对象数组中获取 select 单个字段? - How to select single field from array of json objects? 如何在Ruby on Rails 2.3.14中从某个类的对象数组中选择某个字段 - How do I select a certain field from an array of objects of a certain class in Ruby on Rails 2.3.14 SQL 转换 JSON:从 json 数组对象中选择一个字段 - SQL transform JSON: select one field from json array objects 如何 select 对象数组中的两个属性作为 knex 上的数组? - How to select two properties from array of objects as array on knex? 如何从 AWS Athena 中的 JSON 对象数组中提取字段? - How to extract a field from an array of JSON objects in AWS Athena? 如何根据具有相同 ID 的字段从表中获取 select 值? - How to select values from table based on field with the same id? Select 行,其中嵌套的 json 数组字段包括 PostgreSQL 中提供的数组中的任何值? - Select rows where nested json array field includes any of values from a provided array in PostgreSQL? Postgres:Select 对象数组中值 >= x 以及没有值大于 x 的所有行 - Postgres: Select all rows from array of objects where value >= x and also where no values are greater than x 从2 Field中选择数据并仅使用1个值 - Select data from 2 Field and use only 1 values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM