0

I want to insert multiple values into columns at the same time. For example, I have a JSON data with two rows of data, and I want to insert it at once in my table. I have tried this:

var data = ['{"sensorvalue":"96"},{"sensorvalue":"98"}']

const jsdata = JSON.parse(data);

connection.query("INSERT INTO `nodes`(`sensorvalue`) VALUES ('"+jsdata.sensorvalue+"')", (err, res) => {
    if(err) throw err;
    console.log("counter record inserted");  
}); 

Shows me this error:

{"sensorvalue":"96"},{"sensorvalue":"98"}
                    ^
SyntaxError: Unexpected token , in JSON

The output it should be:

id sensorvalue
1 96
2 98
JoSSte
  • 2,953
  • 6
  • 34
  • 54
ismsm
  • 143
  • 2
  • 11
  • https://stackoverflow.com/questions/8899802/how-do-i-do-a-bulk-insert-in-mysql-using-node-js – Ali Jan 28 '21 at 23:20
  • I would expect it to be `var data = '[{"sensorvalue":"96"},{"sensorvalue":"98"}]'` what you have is an array with a single element containing two objects with a comment in between. i have a string with a json array containing two objects – JoSSte Jan 28 '21 at 23:23
  • @Ali I think this is different not array of arrays instead it is a JSON data. – ismsm Jan 29 '21 at 10:40

1 Answers1

1
var data = ['{"sensorvalue":"96"},{"sensorvalue":"98"}']

should be

var data = '[{"sensorvalue":"96"},{"sensorvalue":"98"}]'

The top is an array with a single element containing two objects with a comma (,) in between. The bottom is a string with a json array containing two objects

JoSSte
  • 2,953
  • 6
  • 34
  • 54