Your data should have some scalar value instead of an array inside the date key.
You can determine what type is a specific key with jsonb_typeof() , and then wrap it inside a CASE statement.
Consider the example of a scalar and an array as your input data set:
select case when jsonb_typeof(jsonb_column->'stats_by_date'->'date') = 'array' then jsonb_array_elements(jsonb_column->'stats_by_date'->'date') else jsonb_column->'stats_by_date'->'date' end as date from ( select '{"stats_by_date": {"date": 123}}'::jsonb
Result
date ------ 123 456
Thus, your request should be written in such a way as to handle such cases:
select id, case when jsonb_typeof(jsonb_column->'stats_by_date'->'date') = 'array' then jsonb_array_elements(jsonb_column->'stats_by_date'->'date') else jsonb_column->'stats_by_date'->'date' end as date from factor_reports_table
source share