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

This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.

NULL ON NULL

Supported by ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The JSON_ARRAYAGG function has a NULL ON NULL clause to indicate that NULL values should be included in the output JSON array:

SELECT json_arrayagg(
  language.description
  NULL ON NULL
)
FROM language
create.select(jsonArrayAgg(
          LANGUAGE.DESCRIPTION)
          .nullOnNull())
      .from(LANGUAGE)
      .fetch();

The result would look like this:

+------------------+
| json_arrayagg    |
+------------------+
| ["English",null] |
+------------------+

Dialect support

This example using jOOQ:

jsonArrayAgg(LANGUAGE.DESCRIPTION).nullOnNull()

Translates to the following dialect specific expressions:

Aurora MySQL, H2, Oracle

json_arrayagg(LANGUAGE.DESCRIPTION NULL ON NULL)

Aurora Postgres, CockroachDB, Postgres, YugabyteDB

json_agg(LANGUAGE.DESCRIPTION)

DuckDB

to_json(array_agg(LANGUAGE.DESCRIPTION))

MariaDB, MySQL

json_merge_preserve(
  '[]',
  concat(
    '[',
    group_concat(coalesce(
      json_quote(LANGUAGE.DESCRIPTION),
      'null'
    ) SEPARATOR ','),
    ']'
  )
)

Snowflake

array_agg(coalesce(
  to_variant(LANGUAGE.DESCRIPTION),
  parse_json('null')
))

Spanner

to_json(
  coalesce(
    array_agg(LANGUAGE.DESCRIPTION), cast(ARRAY[] AS array<string>)
  )
)

SQLite

json_group_array(LANGUAGE.DESCRIPTION)

SQLServer

json_query(('[' + string_agg(coalesce(
  cast(
    ('"' + replace(LANGUAGE.DESCRIPTION, '"', '\"') + '"')
    AS varchar(max)
  ),
  'null'
), ',') + ']'))

Trino

cast(array_agg(LANGUAGE.DESCRIPTION) AS json)

ASE, Access, BigQuery, ClickHouse, DB2, Databricks, Exasol, Firebird, HSQLDB, Hana, Informix, MemSQL, Redshift, SQLDataWarehouse, 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!

The jOOQ Logo