SyntaxError: Unexpected token { in JSON at position 1

Solomon Raja

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

Mayur Dhurpate

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SyntaxError: Unexpected token o in JSON at position 1

"SyntaxError: Unexpected token < in JSON at position 0"

SyntaxError: Unexpected token \ in JSON at position

SyntaxError: Unexpected token T in JSON at position

UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1

NodeJS SyntaxError: Unexpected token in JSON at position 0

SyntaxError: Unexpected token o in JSON at position 1 EXPRESS

Uncaught SyntaxError: Unexpected token o in JSON at position 1 error

SyntaxError: Unexpected token o in JSON at position 1 over ajax request

SyntaxError: Unexpected token } in JSON at position

Jinja Flask issue: Uncaught SyntaxError: Unexpected token { in JSON at position 1 at JSON.parse

how to fix Uncaught SyntaxError: Unexpected token o in JSON at position 1

Uncaught SyntaxError: Unexpected token , in JSON at position 10

SyntaxError: Unexpected token e in JSON at position 1 at JSON.parse (<anonymous>)

How to fix "SyntaxError: Unexpected token o in JSON at position 1"

SyntaxError: Unexpected token ' in JSON at position 0

SyntaxError: Unexpected token } in JSON at position 102

SyntaxError: Unexpected token " in JSON at position 0

SyntaxError - SyntaxError: Unexpected token < in JSON at position 1 - Ionic

SyntaxError: Unexpected token < in JSON at position 1 ionic

"parsererror" SyntaxError: Unexpected token < in JSON at position 0

SyntaxError: Unexpected token / in JSON at position 324

SyntaxError: Unexpected token e in JSON at position 3

Uncaught SyntaxError: Unexpected token a in JSON at position 2

SyntaxError: Unexpected token o in JSON at position 1 in angular

undefined:1 undefined ^ SyntaxError: Unexpected token u in JSON at position 0

SyntaxError: Unexpected token in JSON at position 0 Express

Facing an Uncaught SyntaxError: Unexpected token o in JSON at position 1

SyntaxError: Unexpected token ‘ in JSON at position 0

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive