Available in versions: Dev (3.22) | Latest (3.21) | 3.20 | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14

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 2.25+

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 11+

(('{' || listagg(
  regexp_replace(cast(
    json_object(KEY AUTHOR.FIRST_NAME VALUE AUTHOR.LAST_NAME)
    AS varchar(32672)
  ), '^\{(.*)\}$', '\1'),
  ','
) || '}'))

Databricks

to_json(map_from_entries(array_agg(STRUCT(AUTHOR.FIRST_NAME, cast(AUTHOR.LAST_NAME AS variant)))))

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 10.5+, MySQL 5.7+

json_objectagg(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)

MariaDB 10.2 to 10.4

(concat(
  '{',
  group_concat(regexp_replace(cast(
    json_object(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
    AS char
  ), '^\\{(.*)\\}$', '\\1') SEPARATOR ','),
  '}'
))

SQLite

json_group_object(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')
))

Trino

cast(map(array_agg(AUTHOR.FIRST_NAME), array_agg(AUTHOR.LAST_NAME)) AS json)

ASE, Access, Aurora MySQL, ClickHouse, DB2 9 to 10, Exasol, Firebird, HSQLDB, Hana, Informix, MariaDB 10.0 to 10.1, MemSQL, MySQL 5.6, Redshift, SQLDataWarehouse, SQLServer, Sybase, Teradata, Vertica, YugabyteDB 2.9

/* UNSUPPORTED */
Generated with jOOQ 3.21. 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!

The jOOQ Logo