i have a clients table with 2 columns
CREATE TABLE clients
(
client_id serial primary key,
name VARCHAR(40) not null
)
i have a json data like
[{client_id:"1",name:"Rick"},{client_id:"2",name:"Carlin"}]
Now i need to use this json to parse and insert into my client table. How can i do it using jsp servlets and postgres database.
asked Jun 26, 2014 at 13:38
Dinesh Ravi
1,2352 gold badges18 silver badges36 bronze badges
-
have you looked at yetanotherdataminer.blogspot.co.uk/2013/04/… which gives details on how to load JSON data into a single string per row, then transform?mc110– mc1102014年06月26日 13:44:13 +00:00Commented Jun 26, 2014 at 13:44
-
Also, what version of postgres are you using, as 9.2 added support for JSON inside the DB, and 9.3 added extra functionality for dealing with JSON data.mc110– mc1102014年06月26日 13:46:56 +00:00Commented Jun 26, 2014 at 13:46
-
And ... what have you tried and where are you stuck ?Serge Ballesta– Serge Ballesta2014年06月26日 14:07:20 +00:00Commented Jun 26, 2014 at 14:07
-
@mc110: i am using postgres 9.1.2 which doesnt support this functionalityDinesh Ravi– Dinesh Ravi2014年06月26日 15:36:52 +00:00Commented Jun 26, 2014 at 15:36
-
@SergeBallesta: Jsp page gets this data from client through form which converted into json and passed to the servlet. Now servlet has to insert the data into the databaseDinesh Ravi– Dinesh Ravi2014年06月26日 15:41:24 +00:00Commented Jun 26, 2014 at 15:41
1 Answer 1
If you want to do it on PostgreSQL (9.3+) side, you can use the json_populate_recordset function:
insert into clients
select *
from json_populate_recordset(
null::clients,
'[{client_id:"1",name:"Rick"},{client_id:"2",name:"Carlin"}]'
)
Although, that's usually not a good idea to manually insert values to a serial column.
answered Jun 26, 2014 at 14:12
pozs
36.6k5 gold badges61 silver badges66 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-sql