Earlier quoted context omitted.
Can you elaborate on what they are doing?
I can take a guess. PostgreSQL's JSON tooling makes it much easier to build SQL queries that return different shaped data from different tables in a single query. The row_to_json() function for example turns an entire PostgreSQL row into a JSON object. Here's a query I built that uses that to return results from two different tables, via some CTEs and a UNION ALL: https://simonwillison.net/dashboard/row-to-json/ with…
Imagine having a table with large rows, and another table to join with small data but many rows.
Normally you'd do a inner join of some sort, and the data from "large rows" would be duplicated many many times - json_agg simply fixes this.
you can actually do the full table without json_build_object, too you can do something like
select json_agg(t.) from ( select from table ) as t
this makes joining multiple tables together very very easy, and performant.