JSON_ARRAYAGG
Supported by ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
A data set can be aggregated into a org.jooq.JSON or org.jooq.JSONB array using JSON_ARRAYAGG
SELECT json_arrayagg(author.id) FROM author
create.select(jsonArrayAgg(AUTHOR.ID))
.from(AUTHOR)
.fetch();
The result would look like this:
+---------------+ | json_arrayagg | +---------------+ | [1,2] | +---------------+
Ordering aggregation contents
When aggregating data into an array or JSON array, ordering may be relevant. For this, use the ORDER BY clause in JSON_ARRAYAGG
SELECT json_arrayagg(author.id ORDER BY author.id DESC) FROM author
create.select(jsonArrayAgg(AUTHOR.ID).orderBy(AUTHOR.ID.desc())
.from(AUTHOR)
.fetch();
The result would look like this:
+---------------+ | json_arrayagg | +---------------+ | [2,1] | +---------------+
Dialect support
This example using jOOQ:
jsonArrayAgg(AUTHOR.ID)
Translates to the following dialect specific expressions:
Aurora MySQL, H2, Oracle, SQLDataWarehouse, SQLServer 2025+
json_arrayagg(AUTHOR.ID)
Aurora Postgres, CockroachDB, Postgres, YugabyteDB
json_agg(AUTHOR.ID)
BigQuery, Spanner
to_json(
coalesce(
array_agg(AUTHOR.ID), cast(ARRAY[] AS array<int64>)
)
)
ClickHouse
toJSONString(groupArray(AUTHOR.ID))
DB2 11+
cast(
('[' || listagg(
AUTHOR.ID,
','
) || ']')
AS varchar(32672)
)
Databricks, DuckDB
to_json(array_agg(AUTHOR.ID))
MariaDB 10.4+, MySQL 8.0.19+
json_merge_preserve(
'[]',
concat(
'[',
group_concat(AUTHOR.ID SEPARATOR ','),
']'
)
)
MariaDB 10.2 to 10.3, MySQL 5.7 to 8.0
json_merge(
'[]',
concat(
'[',
group_concat(AUTHOR.ID SEPARATOR ','),
']'
)
)
SQLServer 2017 to 2022
json_query(('[' + string_agg(cast(AUTHOR.ID AS varchar(max)), ',') + ']'))
SQLite
json_group_array(AUTHOR.ID)
Snowflake
array_agg(coalesce(
to_variant(AUTHOR.ID),
parse_json('null')
))
Trino
cast(array_agg(AUTHOR.ID) AS json)
ASE, Access, DB2 9 to 10, Exasol, Firebird, HSQLDB, Hana, Informix, MariaDB 10.0 to 10.1, MemSQL, MySQL 5.6, Redshift, SQLServer 2012 to 2016, Sybase, Teradata, Vertica
/* UNSUPPORTED */
Generated with jOOQ 3.22. Support in older jOOQ versions may differ. Translate your own SQL on our website
References to this page
Feedback
Do you have any feedback about this page? We'd love to hear it!