t9t/jooq-postgresql-json

Repository files navigation

Unfortunately, I no longer use jOOQ or jooq-postgresql-json myself, and so I don't feel comfortable maintaining this library anymore. I will no longer test the library when new jOOQ versions come out, and I won't closely follow jOOQ news anymore to keep an eye out for new features to integrate with.

See also: #24.

Provides jOOQ support for PostgreSQL JSON functions and operators for json and jsonb fields.

Requires at least Java 11.

⚠ Since version 4.0.0 this library does not include a transitive Maven dependency on jooq any more, you have to include both jooq and jooq-postgresql-json in your project to use it.


First, add the following Maven dependency:

<dependency>
  <groupId>com..t9t.jooq</groupId>
  <artifactId>jooq-postgresql-json</artifactId>
  <version>4.0.0</version>
</dependency>

jooq-postgresql-json does not include a transitive dependency on jooq, so you have to include that yourself as well.

As for 4.0.0, this shows only which jOOQ versions are explicitly tested with this library. Minor version differences should still be compatible and newer major versions of jOOQ might still work with older versions of this library if nothing changed much in the jOOQ JSON APIs. New releases will only be created when incompatibilities with new jOOQ versions are found and fixed.

See the changelog for more information about what is included in the various releases and the reason for the breaking changes.

Library versionjOOQ versionNote
4.0.03.16.x - 3.18.xBreaking change, no longer includes a dependency on jooq. Tested with both jOOQ 3.16.20 (JDK 11 and 17), and 3.17.14 and 3.18.5 (JDK 17 only).
3.2.33.16.7
3.2.23.16.6
3.2.13.16.5
3.2.03.16.3
3.1.23.15.5
3.1.13.15.4
3.1.03.15.1
3.0.03.14.12Breaking change, upgraded from Java 8 to Java 11. Java 8 no longer supported.

Use the JsonDSL and JsonbDSL classes to access the JSON functions and operators.

For example, to extract a JSON nested property value as text from a json field:

/* Sample JSON:
{
  "data": {
    "productCode": "Z-5521"
  }
}
*/
String productCode = dsl.select(JsonDSL.extractPathText(MY_TABLE.DATA_FIELD, "data", "productCode"))
    .from(MY_TABLE).fetchOneInto(String.class);

Or for example using the @> operator to update a row of which a jsonb field contains a certain id:

/* Sample JSON:
{
  "id": "1337",
  "name": "The Hitchhiker's Guide to the Galaxy"
}
*/
dsl.update(MY_TABLE)
    .set(MY_TABLE.RATING, 100)
    .where(JsonbDSL.contains(MY_TABLE.DATA_FIELD, JsonbDSL.field("{\"id\": \"1337\"}")))
    .execute()

Kotlin extension functions are available for Field<JSON?> and Field<JSONB?>. That means that instead of something like JsonDSL.extractPathText(MY_TABLE.DATA_FIELD, "data", "productCode") you can instead write: MY_TABLE.DATA_FIELD.extractPathText("data", "productCode").

The extension functions are available in the following packages:

The names of extension functions match the names of the methods on JsonDSL and JsonbDSL, except for concat and contains, which are called concatJson and containsJson respectively to prevent clashes with existing methods of Field.

Reference: https://www.postgresql.org/docs/11/functions-json.html

Operators available for both json (through JsonDSL) and jsonb (through JsonbDSL):

OpOperandDescriptionMethod
->intGet array elementarrayElement()
->textGet object fieldfieldByKey()
->>intGet array element as textarrayElementText()
->>textGet object field as textfieldByKeyText()
#>text[]Get object at pathobjectAtPath()
#>>text[]Get object at path as textobjectAtPathText()

Operators available only for jsonb (through JsonbDSL):

OpOperandDescriptionMethod
@>jsonbDoes contain value?contains()
<@jsonbAre entries contained?containedIn()
?textDoes the key exist?hasKey()
?|text[]Does any key exist?hasAnyKey()
?&text[]Do all keys exist?hasAllKeys()
||jsonbConcatenate valuesconcat()
-textDelete key or elementdelete()
-text[]Delete multiple keys or elementsdelete()
-intDelete array elementdeleteElement()
#-text[]Delete field for pathdeletePath()

Reference: https://www.postgresql.org/docs/11/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE

Processing functions available for both json (through JsonDSL) and jsonb (through JsonbDSL):

FunctionReturn typeDescriptionMethod
json(b)_array_lengthintGet length of JSON arrayarrayLength()
json(b)_extract_pathjson/jsonbExtract object at path (same as #>)extractPath()
json(b)_extract_path_texttextExtract object at path as text (same as #>>)extractPathText()
json(b)_typeoftextGet the type of a JSON fieldtypeOf()
json(b)_strip_nullsjson/jsonbRemove object fields with null valuesstripNulls()

Functions only available for jsonb (through JsonbDSL):

FunctionReturn typeDescriptionMethod
jsonb_prettytextPretty format JSON fieldpretty()

About

jOOQ support for PostgreSQL json & jsonb

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •