我在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。你能否告诉我如何重叠这个问题?
答案 0 :(得分:0)
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)