Answer the question
In order to leave comments, you need to log in
How to insert into a Postgresql table from an array of objects?
Given an array with a large number of objects:
const arryTest = [
{
name: 'name1',
commens: 'commens1',
like: 2,
date: '01,01,2022'
},
{
name: 'name2',
commens: 'commens2',
like: 25,
date: '01,01,2022'
},
{
name: 'name3',
commens: 'commens3',
like: 35,
date: '01,01,2022'
},
]
Answer the question
In order to leave comments, you need to log in
Function: json_array_elements
create table comments (
"name" varchar,
"commens" varchar,
"like" int,
"date" timestamp
);
insert into comments
select (json_populate_record(null::comments, value)).* from json_array_elements('[
{
"name": "name1",
"commens": "commens1",
"like": 2,
"date": "2022-01-01"
},{
"name": "name2",
"commens": "commens2",
"like": 2,
"date": "2022-01-01"
},{
"name": "name3",
"commens": "commens3",
"like": 2,
"date": "2022-01-01"
}
]');
select * from comments;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question