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

喜夏-厌秋 提交于 2021-01-02 06:46:06

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/57271199/how-to-store-json-object-into-postgresql-using-jsonb-data-type-inside-table-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!