【问题标题】:postgres json array elements and null returnspostgres json 数组元素和 null 返回
【发布时间】:2018-05-28 06:17:27
【问题描述】:

我在 postgres 的表中有一个 json 列,其中包含一个对象数组,例如

{"BlockData":[{"Name":"George","Age":"54","Height":"1.75"}, {"Name":"Mario","Age":"35","Height":"1.90"}]}

我正在使用 Select 查询并希望访问 Name 对象和 Name 的值对(George 和 Mario)。我想要做的是:

select jsonb_array_elements(jsondoc_->'BlockData')->>'Name' from BlockData;

我得到的回报是

"ERROR:  cannot extract elements from a scalar

SQL state: 22023"

据我所知,之所以会出现此问题,是因为在某些行中,返回值为 NULL。你能告诉我如何重叠这个问题吗?

【问题讨论】:

    标签: sql arrays postgresql null jsonb


    【解决方案1】:

    您是否尝试过滤它们?

    t=# with t(jsondoc_) as (values('{"BlockData":[{"Name":"George","Age":"54","Height":"1.75"}, {"Name":"Mario","Age":"35","Height":"1.90"}]}'::jsonb),('{"BlockData":null}'))
    select jsonb_array_elements(jsondoc_->'BlockData')->>'Name' from t
    where jsondoc_->'BlockData' <> 'null';
     ?column?
    ----------
     George
     Mario
    (2 rows)
    

    【讨论】:

    • 嗨 Vao 不,我没试过。我现在做了,效果很好,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-01-21
    • 2016-04-09
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2019-01-27
    相关资源
    最近更新 更多