SyntaxError: Unexpected token u at Object.parse (native) NodeJS for JSON.parse()

27,713

Solution 1

Instead of this:

data = req.query.json;
content = JSON.parse(data);        //I am getting the error here

Try for this:

data = req.query.json;
var stringify = JSON.stringify(data)
content = JSON.parse(stringify);       

Solution 2

JSON.parse is choking because unlike Javascript, JSON requires all key names to be in quotes [0], so your JSON should be

{"username":rv,"password":password}

The error "Unexpected token u..." is occurring when the JSON parser encounters the "u" at the beginning of "username", when it expected a quotation mark.

[0] There is a very readable summary of the JSON spec at http://json.org.

Share:
27,713
rv.
Author by

rv.

Updated on July 09, 2022

Comments

  • rv.
    rv. almost 2 years

    I am in the learning stages of NodeJs and I was trying to get values of parameters from the json passed in the URL. I am using body parser because i saw many stack overflow answers using the same to parse through the data.

    Below is the URL I am passing,

    http://localhost:1337/login?json={username:rv,password:password}
    

    I am getting the error mentioned below,

    SyntaxError: Unexpected token u
    at Object.parse (native)
    at C:\Users\summer\Desktop\nodejs\practise3.njs:14:17
    at Layer.handle [as handle_request] (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\route.js:131:13)
    at Route.dispatch (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\index.js:277:22
    at Function.process_params (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\index.js:330:12)
    at next (C:\Users\summer\Desktop\nodejs\node_modules\express\lib\router\index.js:271:10)
    at jsonParser (C:\Users\summer\Desktop\nodejs\node_modules\body-parser\lib\types\json.js:100:40)
    

    The code is mentioned below,

    var express = require('express');
    var http = require('http');
    var app = express();
    var bodyparser = require('body-parser');
    
    app.use(bodyparser.json());
    
    app.get('/login',function(req,res,next){
        var content = '';
        var data = '';
        data = req.query.json;
        content = JSON.parse(data);        //I am getting the error here
        res.writeHead(200,{"Content-type":"text/plain"});
        res.write(data);
        res.end();
    });
    
    http.createServer(app).listen(1337);
    console.log("Server Started successfully at Port 1337");
    

    Note: After reading this question, if you know other alternatives for collecting values from json data, please do tell.

  • rv.
    rv. over 8 years
    I figured out the whole thing and btw both key and value should be in quotes(" ") for JSON parsing to work.
  • Andreas Storvik Strauman
    Andreas Storvik Strauman about 7 years
    Just want to point out that this is dangerous if YOUR_JSON_STRING is user input!