Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert JSON into Postgres via java?

I have created a table books in PostgreSql.

CREATE TABLE books ( id integer, data json );

My Json:

 { "name": "Book the First", "author": { "first_name": "Bob", "last_name": "White" } }

How can i insert json into Postgres via java code?

like image 275
Priyadharsini Avatar asked Jun 13 '26 08:06

Priyadharsini


1 Answers

Thank you :).This really helped me to resolve the issue.

String json = "{ \"name\": \"Book the First\", \"author\": { \"first_name\": \"Priya\", \"last_name\": \"White\" } }";
         PGobject jsonObject = new PGobject();
         jsonObject.setType("json");
         jsonObject.setValue(json);
         PreparedStatement stmt=c.prepareStatement("insert into books values(2,?)");
         stmt.setObject(1, jsonObject);
like image 52
Priyadharsini Avatar answered Jun 15 '26 03:06

Priyadharsini