I'm building a JSON ARRAY from a table which has a JSON column and non-JSON columns.
Here is my sample data and query:
create table check_details(SHORT_NAME VARCHAR, UNIQUE_NO JSON,STATUS VARCHAR);
insert into check_details values('Anu','{"ID":"1e699-76af2"}','REJECTED');
select json_agg(json_strip_nulls(
json_build_object('Name',SHORT_NAME,
'IDS',
jsonb_build_array(case SIGN(position('ACCEPTED' in STATUS) ) when 1 then UNIQUE_NO::jsonb->>'ID' else json_typeof(NULL::json) end)
)))
from check_details;
I am getting this result:
[{"Name":"Anu","IDS":[null]}]
But I do not want to get "IDS":[null] part in my result when the value of the key IDS is NULL. How can I achieve this result:
[{"Name":"Anu"}]
When IDS has a valid value, it has to be an array. Hence using jsonb_build_array.