Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Posting JSON array with multiple objects into multiple rows in an SQL table

I am trying to insert data from an API post request to fill out the rows in a MySQL table. I want the JSON array which has multiple objects in to occupy the rows in the table. I can't seem to figure it out using express JS. I am hoping someone can assist me with this problem.

Expected Result:

enter image description here

Below is my JS code snippet:

app.post('/dump', (req,res) => {
    let info = req.body;
    var sql = "INSERT INTO `testStuff` (`j_object`) VALUES(?)";
    mysqlConnection.query(sql,[req.body],(err,rows,fields) => {
        if (!err)
            res.send('insert success');
        else
            console.log(err)
    }) 
});

Example Post Body:

{
    "test": [
        {
            "id": 20,
            "name": "John Doe",
            "location": "NY"
        },
        {
            "id": 21,
            "name": "Betty Doe",
            "location": "FL"
        }
    ]
}

MYSQL Table:

CREATE TABLE `testStuff` (
    `j_object` json NOT NULL
)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

INSERT INTO testStuff (j_object)
SELECT value
FROM JSON_TABLE(@post_body, '$.test[*]' COLUMNS (value JSON PATH '$')) jsontable

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b8394f7e3e80942fcae063441bed7af1

In var sql = ... use ? instead of @post_body.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!