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?
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?
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);
I have another one doubt.How to split JSON key value pair and insert into postgreSql table's column.
For example my JSON looks like { "Name":"xyz" "salary":10000 }
I want to insert into separate columns Name and salary in Postgres.