JSONB_AGG
Collects values from many rows into one JSON array.
Syntax
JSONB_AGG(<expression>)
JSONB_AGG(<expression> ORDER BY <sort key>)
Like ARRAY_AGG, but the result is a JSON array that's easy to send straight to the application. Together with JSONB_BUILD_OBJECT it builds whole lists of objects: a department and all its people in one field.