Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store JSON object into PostgreSQL using JSONB data type inside table and PostgreSQL JDBC driver

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?

like image 309
Swanand Keskar Avatar asked Jul 30 '19 12:07

Swanand Keskar


People also ask

Can I store JSON data in PostgreSQL?

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.

Can we insert JSON data into PostgreSQL?

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.

What is Jsonb data type in PostgreSQL?

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.

How do I query Jsonb data in PostgreSQL?

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.


1 Answers

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

like image 114
Swanand Keskar Avatar answered Oct 03 '22 12:10

Swanand Keskar