3

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?

2
  • Run an insert using a PreparedStatement Commented Dec 8, 2016 at 6:59
  • Hope this will be useful stackoverflow.com/questions/35844138/… Commented Dec 8, 2016 at 7:10

2 Answers 2

7

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);
0

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.

3
  • You must parse the json string Commented Dec 8, 2016 at 10:00
  • Still having doubts on this.how to split JSON using parser?can you pls explain it Commented Dec 9, 2016 at 6:22
  • The process will depend on the parser you choose Commented Dec 9, 2016 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.