Reputation: 89
I'm trying to get a Jooq Table record from json produced with postgres row_to_json().
I have a Table called InventoryItem in Postgres with the following schema:
CREATE TABLE "InventoryItem" (
"id" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"price_per_100_units" double precision,
"weight_in_kilograms" double precision,
"inventory_count" integer,
"expected_value" double precision,
"description" text
);
When I use row_to_json() in Postgres to create a Json for a single row from this table, I get:
{
"id": 6,
"price_per_100_units": null,
"weight_in_kilograms": null,
"inventory_count": null,
"expected_value": 142,
"description": null
}
I couldn't find a way to take the above Json and convert it into an instance of Jooq InventoryItemRecord.
I tried to use Jooq's import Json functionality, but that only works for the Json format produced from Jooq export to Json. It doesn't work with row_to_json() output.
As a last resort, I could write a Pojo to which I deserialize the above json and then create JooqRecord from this pojo, but this seems like a lot of work for multiple tables.
How does one convert row_to_json() output to a Jooq TableRecord?
Upvotes: 1
Views: 525
Reputation: 89
Adding my comment as a full answer to be clear.
Jooq by default seems to use the array format when exporting a record to json. If you're trying to interface postgres row_to_json()
, you need to tell Jooq to use Json object format when exporting to json.
I was able to achieve this by:
JSONFormat jsonFormat = new
JSONFormat().header(false).recordFormat(JSONFormat.RecordFormat.OBJECT);
Specifying RecordFormat.OBJECT
does the trick.
Upvotes: 0
Reputation: 220952
You can use DSLContext::fetchFromJSON
to load any jOOQ supported JSON format into a jOOQ Result
, e.g.
Object v = ctx.fetchValue(
"select row_to_json(x) from inventory_item as x where id = 1");
InventoryItemRecord record = ctx.fetchFromJSON("[" + v + "]")
.into(INVENTORY_ITEM)
.get(0);
You're probably looking for this convenience feature, which isn't available yet in jOOQ 3.17:
But the above is almost equivalent, except that you have to:
Record
containing the JSON data into an InventoryItemRecord
manually.Upvotes: 1