I want to save following json object into PostgreSQL db table as jsonb
{
"fname":"john",
"lname:"doe",
}
I am currenlty using PGObject to create object and set type to jsonb and pass value as json string
Looking for a better approach with micronaut-data and micronaut Is there any native data type supported in micronaut-data to convert the Java object to JSON and store in db? How to save the data using postgres jdbc driver
typecasting in query using :jsonb is already tried with raw jdbc if it works with micronaut-data / predator how to do it?
PostgreSQL offers two types for storing JSON data: json and jsonb . To implement efficient query mechanisms for these data types, PostgreSQL also provides the jsonpath data type described in Section 8.14. 7. The json and jsonb data types accept almost identical sets of values as input.
PostgreSQL allows you to work on both JSON and SQL data to implement relational and non-relational queries. This way, PostgreSQL allows you to leverage SQL commands for processing data stored in tables of respective database servers.
PostgreSQL jsonb is the extended version data type of json data type in PostgreSQL, the main difference of json and jsonb data type is json will store the data into the plain text format while jsonb data type will store the data in binary format.
Querying the JSON documentPostgreSQL has two native operators -> and ->> to query JSON documents. The first operator -> returns a JSON object, while the operator ->> returns text. These operators work on both JSON as well as JSONB columns. There are additional operators available for JSONB columns.
We can use PGObject and Jackson Object mapper to save the json in Postgres
val person=PGobject()
person.type="jsonb"
person.value=ObjectMapper.writeValueAsString("{"fname":"john","lname":"doe"}")
now person object can be passed to JDBC driver for storing data as jsonb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With