Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda