Available in versions: Dev (3.21)

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.

Optional JSON entry expressions

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

An occasionally useful capability when creating dynamic SQL queries is to be able to provide optional JSON_OBJECT function arguments. You can do this using DSL.noJsonEntry():

boolean condition = ...

create.select(jsonObject(
            key("id").value(BOOK.ID),
            
            // The following two are equivalent
            key("title1").value(condition ? BOOK.TITLE : noField()),
            condition ? key("title2").value(BOOK.TITLE) : noJsonEntry()
       ))
      .from(BOOK)
      .fetch();

The above query produces:

-- If condition is true
SELECT json_object(KEY id VALUE book.id, KEY title1 VALUE book.title, KEY title2 VALUE book.title)
FROM book

-- If condition is false
SELECT json_object(KEY id VALUE book.id)
FROM book
The noJsonEntry() expression is supported only in the DSL API, not in the model API, where the behaviour of noJsonEntry() is undefined.

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo