+ 3

JSON returning syntax error while syntax seems to be correct

I have a JSON file which contains an array with two objects in it like this: [ { "firstname":"John1", "lastname":"Doe1" }, { "firstname":"John2", "lastname":"Doe2" } ] Though, in my file, everything is written in one line like this: [{"firstname":"John1","lastname":"Doe1"},{"firstname":"John2","lastname":"Doe2"}] I don't see any syntax error here, though my Node.js server logs me this in the command prompt when I try to read the JSON file: SyntaxError: Unexpected token , in JSON at position xxx Here that "," is the exact comma separating the 2 objects inside that array. Any ideas what's the issue here? Thanks.

2nd Sep 2020, 6:26 PM
robert1
robert1 - avatar
13 Answers
+ 1
Oh guys, hang on. I just fixed the whole thing. I shouldn't have removed the square brackets, because that makes the JSON file invalid, which obviously generates that syntax error. To make the thing work that I want to be working, I had to refer to a specific element in the array. I was just a little stupid here by removing the square brackets and thinking it would refer to the first object automatically.
3rd Sep 2020, 12:46 PM
robert1
robert1 - avatar
+ 2
Yes
3rd Sep 2020, 12:46 PM
Harsh Mathur (Exams soon )
+ 2
My software is also showing it invalid
3rd Sep 2020, 12:47 PM
Harsh Mathur (Exams soon )
+ 1
I have a bit experience of Json By Mcpe modding Can you tell That position Or its just showing xxx
3rd Sep 2020, 7:54 AM
Harsh Mathur (Exams soon )
+ 1
Harsh Mathur I replaced the number with xxx because my code is different than the one I described in the question. The position is pointing to the comma that separates the 2 objects inside the array.
3rd Sep 2020, 8:04 AM
robert1
robert1 - avatar
+ 1
Harsh Mathur I just replaced the firstname value and the lastname value with different names. Further on the code structure is EXACTLY the same. I cannot show you the code rn, I will drop it here later.
3rd Sep 2020, 8:10 AM
robert1
robert1 - avatar
+ 1
Ok no prob
3rd Sep 2020, 8:18 AM
Harsh Mathur (Exams soon )
+ 1
Let me see
3rd Sep 2020, 8:18 AM
Harsh Mathur (Exams soon )
+ 1
Hmmm this code is Right there are no mistakes in it Maybe its a system error or something like that because The code is Right
3rd Sep 2020, 8:23 AM
Harsh Mathur (Exams soon )
+ 1
Harsh Mathur I will drop the code later together with my Node.js where I read the JSON file.
3rd Sep 2020, 8:24 AM
robert1
robert1 - avatar
+ 1
Hmm you should try other application or software because the problem is in it maybe
3rd Sep 2020, 8:28 AM
Harsh Mathur (Exams soon )
+ 1
Hi Harsh Mathur. So, here's the code. In Node.js, this is the code that has to do with reading the file: let body = ''; req.on('data', (chunk) => body += chunk); req.on('end', () => { const userNew = qs.parse(body.toString()); if(fs.existsSync('formdata.json') != false) { var usersData = JSON.parse(fs.readFileSync('formdata.json').toString().replace(/\[/g,'').replace(/\]/g,'')); var users = []; users.push(userNew,usersData); fs.writeFile('formdata.json', JSON.stringify(users), (err) => {if(err) throw err;}); res.end(); } else { //smth else } (Sorry if it's hard to read). And again, this is the JSON file: [{"firstname":"John1","lastname":"Doe1","email":"[email protected]","submit":"Register"},{"firstname":"John2","lastname":"Doe2","email":"[email protected]","submit":"Register"}] And this is the feedback I get in the command prompt: undefined:1 <the content of the JSON file> SyntaxError: Unexpected token , in JSON at position 106 Position 106 is the position of that comma that separates the 2 objects in the array in my code.
3rd Sep 2020, 12:02 PM
robert1
robert1 - avatar
0
So pls give the real code i also have to see because
3rd Sep 2020, 8:05 AM
Harsh Mathur (Exams soon )