I\'m using postgres 9.4, I have the following query:
SELECT pid, code, name FROM activity, ( SELECT code FROM project ) projects WHERE activity.pcode = project
select pid, json_agg(json_build_object('code',code,'name',name)) from ... group by pid