By using jsonb_array_elements()
function to extract out jsonb
data array from Postgres, it gave error:
cannot extract elements from a scalar
I assume that it is because of the NULL
in the return call, added the NULL
checking condition but not work. Any help appreciated.
select id ,
CASE
WHEN report IS NULL OR
(report->'stats_by_date') IS NULL OR
(report->'stats_by_date'-> 'date') IS NULL then to_json(0)::jsonb
ELSE jsonb_array_elements(report -> 'stats_by_date' -> 'date')
END AS Date
from factor_reports_table
The truncated json array looks like:
"stats_by_date": {"date": [16632, 16633, 16634, ...], "imps": [2418, 896, 1005...], ...}
jsonb_array_elements
is a scalar, not a json array. – Kamil G. Aug 30 at 20:56jsonb_array_elements
function. Want to know how to check and bypass it – Hai Qu Aug 30 at 21:15