DO $$ DECLARE _a1 jsonb; _a2 jsonb; _a3 jsonb; BEGIN _a1 := (select '["A","B","A"]'::jsonb); _a2 := (select '["A","B","A"]'::jsonb); SELECT jsonb_agg(DISTINCT x.ae) INTO _a3 FROM ( SELECT jsonb_array_elements(_a1) ae UNION ALL SELECT jsonb_array_elements(_a2) ae ) x; raise notice '%', _a3; END; $$ language plpgsql