Fix – Unexpected token u in JSON at position 0 Error in JS

Unexpected token b in json at position 0: When we use the JSON.parse or $.parseJSON methods with an undefined value, we get the “Unexpected token u in JSON at position 0” error. Before parsing the value you’re trying to parse, inspect it to ensure it’s a proper JSON string.

Below are the examples where this “Uncaught SyntaxError: Unexpected end of Input” occurs and how to fix that.

Fix – Unexpected token u in JSON at position 0 Error in JS

Cause 1: Undefined Values

// SyntaxError: Unexpected token u in JSON at position 0
// Here we are passing undefined value to the parse() function and apply it on the JSON and print it using console.log()
console.log(JSON.parse(undefined));

// SyntaxError: Unexpected token u in JSON at position 0
// Here we are passing undefined value to the parseJSON() function and getting its value using $ and print it using console.log()
console.log($.parseJSON(undefined));

Output:

undefined:1
undefined
^

SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (C:\Users\cirus\Desktop\pyrebase4\index.js:3:18)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47

When an undefined value is passed to the JSON.parse method, it is converted to a string, with the letter u as the first character, indicating the error message.

When executing JSON.parse, the error “Unexpected token u in JSON at position 0” can happen for a variety of reasons:

  • A non-existent property on an object is being referenced.
  • An empty response was returned by your server or local storage call.
  • You’re retrieving data as soon as the page loads, resulting in a race issue.

Fix 1: Using try-catch block

You can put the JSON.parse call in a try/catch to ensure that you handle the error.

//try catch block
try {
    // pass the undefined value as an argument to the parse() function of the JSON and store this result in a variable
  const reslt = JSON.parse(undefined);
} catch (err) {
  // Print the error message in catch block using the message attribute
  console.log('Error: ', err.message);
}

Output:

Error: Unexpected token u in JSON at position 0

The try block attempts to execute the block of statements included within it, but when an error occurs, the catch block is invoked, and the error message is printed in the catch block.

Note:

JSON values must all be of the string type. Although not all strings are valid JSON, all JSON values have a string type.

Cause #2: Incorrect Spellings

When you try to parse a property that doesn’t exist, you’ll get this error. This is usually due to a mistake or referring the incorrect property (one that is not parsable).
Have you noticed the mistake? We’re trying to parse my.data, but we’re referring a non-existent property, my.date, which is undefined, owing to a typo. That there  is a “unexpected token u in JSON at position 0”

Another less typical cause of this problem is because the JSON is not received. This could be due to a client-side script that ignores errors and sends requests when they aren’t supposed to be sent. You’ll get an error each time this happens because there’s no data to pass to the request.

// Creating sample Object
my = {}
//sample json object
my.dataVal = '{"website":"BTechGeeks"}';
// Pass the above dataVal as an argument to the parse() function
JSON.parse(my.dateVal)

Output:

undefined:1
undefined
^

SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (C:\Users\cirus\Desktop\pyrebase4\index.js:5:6)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47

Fix #2: Giving Correct Spellings

// Creating sample Object
my = {}
//sample json object
my.dataVal = '{"website":"BTechGeeks"}';
// Pass the above dataVal as an argument to the parse() function
console.log(JSON.parse(my.dataVal))

Output:

{ website: 'BTechGeeks' }

Fix #3: Removing unwanted white Spaces

Remove any white space that could cause your data to become corrupted. Here’s an example:

//removing unwanted whitespaces using the trim() function
dataValues = dataValues.trim(); 
result= JSON.parse(dataValues );

Fix #4: Clearing local storage

Clear local storage (browser console -> localStorage.clear()) and try again if you’re using it to store your info (remember to rewrite your data if this was done manually).

localStorage.clear()