Thank you -
On Tue, Mar 20, 2018 at 3:27 PM, Tom Lane <tgl@xxxxxxxxxxxxx> wrote:
I think you could push the conditionality into a plpgsql function,
something like (untested)
create function jsonb_elements_if_array(j jsonb) returns setof jsonb as $$
begin
if jsonb_typeof(j) = 'array' then
return query select jsonb_array_elements(j);
end if;
end$$
strict immutable language plpgsql;
Note that this gives *no* elements, rather than a single NULL value,
if the input isn't an array --- but that seems to me to make more sense
than your existing code anyhow. If you disagree, add "else return next
null::jsonb".
I think I will just introduce a separate column (until now I was trying to squeeze 2 different kinds of data - JSON array of objects and a string - into the one column)... I believe Adrian had suggested it before :-)