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
3
  • stackoverflow.com/questions/8899802/… Commented Jan 28, 2021 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 Commented Jan 28, 2021 at 23:23
  • @Ali I think this is different not array of arrays instead it is a JSON data. Commented Jan 29, 2021 at 10:40

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But, I'm still I can not insert it as two rows.
0

this should work:

var data = ["sensorvalue":"96",
            "sensorvalue":"98"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.