PostgreSQL Query output to JSON
It's a bit hard to follow your sample, but something along the lines should do it:
select to_jsonb(main)||ch.data
from main_table main 
  join (
     select main_id, jsonb_agg(to_jsonb(child)) as data
     from child_table child
     group by main_id
  ) ch on ch.main_id = main.id

If you want everything as one gigantic JSON array, use select jsonb_agg(to_jsonb(main)||ch.data) in the outer select.