Available in versions: Dev (3.22) | Latest (3.21) | 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, SQLDataWarehouse, SQLServer 2025+

json_arrayagg(LANGUAGE.DESCRIPTION NULL ON NULL)

Aurora Postgres, CockroachDB, Postgres, YugabyteDB 2.25+

json_agg(LANGUAGE.DESCRIPTION)

DuckDB

to_json(array_agg(LANGUAGE.DESCRIPTION))

MariaDB 10.4+, MySQL 8.0.19+

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

MariaDB 10.2 to 10.3, MySQL 5.7 to 8.0

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

SQLServer 2017 to 2022

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

SQLite

json_group_array(LANGUAGE.DESCRIPTION)

Snowflake

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

Spanner

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

Trino

cast(array_agg(LANGUAGE.DESCRIPTION) AS json)

ASE, Access, BigQuery, ClickHouse, DB2, Databricks, Exasol, Firebird, HSQLDB, Hana, Informix, MariaDB 10.0 to 10.1, MemSQL, MySQL 5.6, Redshift, SQLServer 2012 to 2016, Sybase, Teradata, Vertica, YugabyteDB 2.9

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