如何从postgres中的json数组对象查询值

时间:2021-03-26 09:35:05

标签: json postgresql

我的一个列响应将这样的值存储在 postgres 表数据库中:

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
    if ("text/plain".equals(type)) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // Update UI to reflect text being shared
        }
    }
}

我想返回cin值。 我试过了

{"result": [{"cin": "134234", "entityName": "xyz"}, {"cin": "2452352", "entityName": "uvw"}]

但它返回null;

谁能给我一个简单的解决方案。

1 个答案:

答案 0 :(得分:0)

您可以为此使用 JSON 路径查询:

select sale_date, sum(total_amount) as total_amount,
   sum(credit_amount) as credit_amount,
   sum(total_amount) - sum(credit_amount) as cash_amount
from (
    select sale_date, total_amount, 0 as credit_amount
    from sales 
    union all
    select credit_sale_date, 0 as total_amount, amount as credit_amount
    from credit_sales
) t
group by sale_date

这假设 select jsonb_path_query_first(response, '$.result[*] ? (@.entityName == "xyz").cin') from test where id = 1; 是一个 response 列(它应该是)。如果不是,则需要将其投射 jsonb

结果又是一个JSONB值