简体   繁体   中英

SyntaxError: Unexpected token { in JSON at position 1

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 ()

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" } ] 

You 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 (eg empid ) though iterating through them might not be as easy as an array.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM