Postgres json_agg() function
Aggregate values into a JSON array
The Postgres json_agg()
function is an aggregate function that collects values from multiple rows and returns them as a single JSON array.
It's particularly useful when you need to denormalize data for performance reasons or prepare data for front-end applications and APIs. For example, you might use it to aggregate product reviews for an e-commerce application or collect all posts by a user on a social media platform.
Function signature
The json_agg()
function has this simple form:
expression
: The value to be aggregated into a JSON array. This can be a column, a complex expression, or even a subquery.
When used in this manner, the order of the values in the resulting JSON array is not guaranteed. Postgres supports an extended syntax for aggregating values in a specific order.
expression
: The value to be aggregated into a JSON array.ORDER BY
: Specifies the order in which the values should be aggregated.sort_expression
: The expression to sort by.ASC | DESC
: Specifies ascending or descending order (default is ASC).NULLS { FIRST | LAST }
: Specifies whether nulls should be first or last in the ordering (default depends onASC
orDESC
).
Example usage
Consider an orders
table with columns order_id
, product_name
, and quantity
. We can use json_agg()
to create a JSON array of all products in each order.
This query groups the orders by order_id
and creates a JSON array of products for each order.
Advanced examples
Ordered aggregation
You can specify an order for the aggregated values, as suggested in the function signature section. Here's an example:
This query aggregates product reviews into a JSON array, ordered by the review date in descending order.
Combining with other JSON functions
json_agg()
can be combined with other JSON functions for more complex transformations:
This query uses json_build_object()
in combination with json_agg()
to create an array of quarterly sales data, for each region.
Additional considerations
Performance implications
While json_agg()
is powerful for creating JSON structures, it can be memory-intensive for large datasets since its output size linearly increases with the number of rows. When working with very large tables, consider using pagination or limiting the number of rows aggregated.
Alternative functions
array_agg()
: Aggregates values into a Postgres array instead of a JSON array.jsonb_agg()
: Similar tojson_agg()
, but returns ajsonb
type, which is more efficient for storage and processing.json_agg_strict()
: Aggregates values into a JSON array, skipping over the NULL values.
Resources
Last updated on