I am trying to get the JSON request which has inner objects with out key. But I am getting Unexpected token { at position 1
The sample JSON is given below.
{ { "empid" : "001", "academictype": "Bachelor", "academicdegree": "BE", "academicSpecialization": "Computer Science" }, { "empid": "002", "academictype": "Masters", "academicdegree": "MBA", "academicSpecialization": "Human Resources" } }
I have tried with below code
createEmployeeAcademics(req, res, next) {
let body = '';
var fbResponse = [];
req.on('data', function (chunk) {
console.log(chunk);
body += chunk;
console.log(body);
});
req.on('end', function () {
fbResponse.length = 0;
var arrayValues = JSON.parse(body);
for (var i = 0; i < arrayValues.length; i++) {
fbResponse.push(arrayValues[i]);
}
});
}
I am getting below error
SyntaxError: Unexpected token { in JSON at position 1 at JSON.parse ()
I am trying to get the JSON request which has inner objects with out key. But I am getting Unexpected token { at position 1
The sample JSON is given below.
{ { "empid" : "001", "academictype": "Bachelor", "academicdegree": "BE", "academicSpecialization": "Computer Science" }, { "empid": "002", "academictype": "Masters", "academicdegree": "MBA", "academicSpecialization": "Human Resources" } }
I have tried with below code
createEmployeeAcademics(req, res, next) {
let body = '';
var fbResponse = [];
req.on('data', function (chunk) {
console.log(chunk);
body += chunk;
console.log(body);
});
req.on('end', function () {
fbResponse.length = 0;
var arrayValues = JSON.parse(body);
for (var i = 0; i < arrayValues.length; i++) {
fbResponse.push(arrayValues[i]);
}
});
}
I am getting below error
Share Improve this question asked May 2, 2019 at 9:19 Solomon RajaSolomon Raja 1,8102 gold badges16 silver badges24 bronze badges 4SyntaxError: Unexpected token { in JSON at position 1 at JSON.parse ()
- JSON "is a collection of name/value pairs", so you have invalid JSON, which is why it's not parsing – George Commented May 2, 2019 at 9:23
- @George — That website is out of date. The description you give is correct for an object in JSON, which is what we have here, but not for JSON in general. – Quentin Commented May 2, 2019 at 9:27
- @Quentin Didn't realise, can you remend an up to date on? Still it's still a good base mark to learn what JSON is it you don't know. – George Commented May 2, 2019 at 9:33
- 1 The current specification is tools.ietf/html/rfc7159 – Quentin Commented May 2, 2019 at 9:34
4 Answers
Reset to default 4You are getting Unexpected token
error because JSON.parse(body)
is unable to parse the JSON provided by you. Prettifying the JSON using online tools can help debug this issue better. FOr your string:
{
{
"empid": "001",
"academictype": "Bachelor",
"academicdegree": "BE",
"academicSpecialization": "Computer Science"
}, {
"empid": "002",
"academictype": "Masters",
"academicdegree": "MBA",
"academicSpecialization": "Human Resources"
}
}
Your JSON is missing an important property related to objects
.
JSON objects are surrounded by curly braces {}.
JSON objects are written in key/value pairs.
Your outermost curly braces are not followed by any key-value pair, and rather directly starts with another object. You can correct te JSON by either:
1. Making outer JSON an array
[
{
"empid": "001",
...
}, {
"empid": "002",
...
}
]
An array can directly have the objects as their children and it can be accessed sequentially.
2. Add keys to the children objects of the outer object:
{
"001": {
"academictype": "Bachelor",
"academicdegree": "BE",
"academicSpecialization": "Computer Science"
},
"002": {
"academictype": "Masters",
"academicdegree": "MBA",
"academicSpecialization": "Human Resources"
}
}
This way you can access the objects directly using their unique IDs (e.g. empid
) though iterating through them might not be as easy as an array.
This is not valid Java script object or list and already parsed. try to make list then loop only
like
[ { "empid" : "001", "academictype": "Bachelor", "academicdegree": "BE", "academicSpecialization": "Computer Science" }, { "empid": "002", "academictype": "Masters", "academicdegree": "MBA", "academicSpecialization": "Human Resources" } ]
Your sample JSON is invalid. You can use json lint to validate your json object.
The sample JSON which you have shown is not valid JSON. If that is how your server is providing the data, then the server is providing wrongly formatted JSON.
You may have to fix the server to send the data in a key-value-pairs format.
There are two ways to do it.
1) Put the array of employees in a wrapper object:
{"employees": [{ "empid" : "001", "academictype": "Bachelor", "academicdegree": "BE", "academicSpecialization": "Computer Science" }, { "empid": "002", "academictype": "Masters", "academicdegree": "MBA", "academicSpecialization": "Human Resources" }]}
2) Put the employees mapped by the employee ids:
{ "001":{ "empid" : "001", "academictype": "Bachelor", "academicdegree": "BE", "academicSpecialization": "Computer Science" }, "002":{ "empid": "002", "academictype": "Masters", "academicdegree": "MBA", "academicSpecialization": "Human Resources" } }
Depending on which format you pick, the client side code will differ slightly.