JSON_OBJECTAGG
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 object using JSON_OBJECTAGG
SELECT json_objectagg( CAST(author.id AS varchar(100)), first_name ) FROM author
create.select(jsonObjectAgg(
cast(AUTHOR.ID, VARCHAR(100)),
AUTHOR.FIRST_NAME
))
.from(AUTHOR)
.fetch();
The result would look like this:
+----------------------------+
| json_objectagg |
+----------------------------+
| {"1":"George","2":"Paulo"} |
+----------------------------+
Dialect support
This example using jOOQ:
jsonObjectAgg(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Translates to the following dialect specific expressions:
Aurora Postgres, Postgres, YugabyteDB
json_object_agg(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
BigQuery, Spanner
json_object( array_agg(AUTHOR.FIRST_NAME), array_agg(AUTHOR.LAST_NAME) )
CockroachDB
(('{' || string_agg(regexp_replace(cast(
json_build_object(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
AS string
), '^\{(.*)\}$', '\1', 'g'), ',') || '}'))
DB2
(('{' || listagg(
regexp_replace(cast(
json_object(KEY AUTHOR.FIRST_NAME VALUE AUTHOR.LAST_NAME)
AS varchar(32672)
), '^\{(.*)\}$', '\1'),
','
) || '}'))
DuckDB
to_json(map_from_entries(array_agg(ROW(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME))))
H2, Oracle
json_objectagg(KEY AUTHOR.FIRST_NAME VALUE AUTHOR.LAST_NAME)
MariaDB, MySQL
json_objectagg(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Snowflake
object_agg(coalesce(
to_variant(AUTHOR.FIRST_NAME),
parse_json('null')
), coalesce(
to_variant(AUTHOR.LAST_NAME),
parse_json('null')
))
SQLite
json_group_object(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
Trino
cast(map(array_agg(AUTHOR.FIRST_NAME), array_agg(AUTHOR.LAST_NAME)) AS json)
ASE, Access, Aurora MySQL, ClickHouse, Databricks, Exasol, Firebird, HSQLDB, Hana, Informix, MemSQL, Redshift, SQLDataWarehouse, SQLServer, Sybase, Teradata, Vertica
/* UNSUPPORTED */
Generated with jOOQ 3.21. Support in older jOOQ versions may differ. Translate your own SQL on our website
Feedback
Do you have any feedback about this page? We'd love to hear it!