+ 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.
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.
+ 2
Yes
+ 2
My software is also showing it invalid
+ 1
I have a bit experience of Json By Mcpe modding Can you tell That position Or its just showing xxx
+ 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.
+ 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.
+ 1
Ok no prob
+ 1
Let me see
+ 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
+ 1
Harsh Mathur I will drop the code later together with my Node.js where I read the JSON file.
+ 1
Hmm you should try other application or software because the problem is in it maybe
+ 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.
0
So pls give the real code i also have to see because
Hot today
I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.
0 Votes
Тренажер кода
0 Votes
Python palindrome challenge.
1 Votes
Java
0 Votes