Jooq fetchInto with default value if field is null

The reason is that JSON_ARRAYAGG() (like most aggregate functions) produces NULL for empty sets, instead of a more “reasonable” empty []. Clearly, you never want this behaviour. So, you could use COALESCE, instead, see also this question:

coalesce(
  jsonArrayAgg(jsonObject(book.ID, book.PRICE)),
  jsonArray()
)

I’ll make sure to update all the other answers I’ve given on Stack Overflow to point this out. A future jOOQ version might offer NULL safe aggregate functions in case there exists a reasonable identity for aggregation (e.g. []) to make this more discoverable: https://github.com/jOOQ/jOOQ/issues/11994

Leave a Comment