I wanted to use array_agg to eliminate nulls before converting to JSON but the null reappears in the JSON output. Here is a minimal example demonstrating the behaviour:
select id, array_agg(alias), array_to_json(array_agg(alias))
from (values (1, 'foo'), (1, 'bar'), (2, null)) t(id, alias)
group by id;
The resultset is this:
id|array_agg|array_to_json|
--+---------+-------------+
1|{foo,bar}|["foo","bar"]|
2|{} |[null] |