Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert json data into my table using postgres

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.

like image 693
Dinesh Ravi Avatar asked Mar 19 '26 06:03

Dinesh Ravi


1 Answers

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.

like image 194
pozs Avatar answered Mar 21 '26 21:03

pozs