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

syntaxerror: unexpected token in json at position 0 nodejs
unexpected token u in json at position 0 javascript
unexpected token u in json at position 0 angular
json.parse unexpected token
unexpected token o in json at position 1
unexpected token < in json at position 0 angular 6
unexpected token u in json at position 0 postman
unexpected token in json at position 0 express

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.

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

TIL Thursday: Troubleshooting 'Uncaught SyntaxError: Unexpected , Uncaught SyntaxError: Unexpected token u in JSON at position 0 If you attempt to parse something other than an object, the JavaScript Finally, a fun tip that relates to JSON.parse() and JSON.stringify() : if you've ever  The parameter for the JSON.parse may be returning nothing (i.e. the value given for the JSON.parse is undefined)!. It happened to me while I was parsing the Compiled solidity code from an xyz.sol file.

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.

Json parse unexpected token, Unexpected token u in JSON at position 0 when sending info to other js file - json. token u in JSON at position 0 at Object.parse (native) at Request.request.get [​as _callback] (D:\Projects\NodeJS\WoWBot-Discord\lib\user.js:12:27) at functions.config().elasticsearch; let elasticSearchUrl = elasticSearchConfig.url +  I just started working with node.js and json objects in my college course. One of our assignments this week was to create a few json objects and extract parts of the object into an html page.

This happens because the key name must be enclosed within double quotes. Here is a workaround to overcome this issue:

var jsonObj = eval('(' + YOUR_JSON_STRING + ')');

Unexpected token u in JSON at position 0 when sending info to , fixing SyntaxError: Unexpected string/token in JSON at position. The v8 JSON parser, used in Node.js and in the Chrome browser's javascript parser. You may encounter errors like the following when the input JSON string is malformed: function, or libraries in other languages, to create JSON strings from real objects. checking if the job exists in updateDelaySet will resolve the symptom, but the underlying problem is a hazard of some sort. I am speculating that the call to remove job happened after the job has been read and just before calling job.moveToDelayed which in turn calls job##_moveToSet.

fixing SyntaxError: Unexpected string/token in JSON at position , uncaught syntaxerror: unexpected token < in json at position 0 at json.parse (<​anonymous>) unexpected token I'm writing my first application in Node.js. at Object.parse (native) You have a strange char at the beginning of the file. data. Fix JSON.parse Unexpected Token If you’re using JSON.parse directly, that’s a plain old synchronous call and you can replace the call with a console.log to see what’s going on. // JSON.parse(someString) // comment this out temporarily console . log ( someString ) // log out out, and see what's wrong

JSON.parse() causes error: `SyntaxError: Unexpected token in , SyntaxError: Unexpected token u in JSON at position 0 #721 from pending to an active state (), the job's properties are converted from JSON to scalar values. end of JSON input at JSON.parse (<anonymous>) at Object. > JSON.parse(undefined) SyntaxError: Unexpected token u I'm now wondering if this is related to #139 or expressjs/express#1097. Obviously, if Faye receives a request with no parameters it should treat the request as invalid, but I'm not sure why the request is empty.

SyntaxError: Unexpected token u in JSON at position 0 · Issue #721 , As for the "unexpected token u", this occurs when you try to JSON.parse() a string open sourced an automated irrigation system based on Node.js and React. Kevin Leary, Boston WordPress Developer of 10 Years. I'm a freelance web developer and WordPress consultant in Boston, MA with 13 years of experience building websites and applications.

Comments
  • I figured out the whole thing and btw both key and value should be in quotes(" ") for JSON parsing to work.
  • Just want to point out that this is dangerous if YOUR_JSON_STRING is user input!