Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda