isotropic-2022
Share
Blog
Search
Menu

How To Fix Unexpected token u in JSON at position 0

By James LePage
|
Last updated on March 24th, 2022
|

The "Unexpected token u in JSON at position 0" is a typically Javascript error that will show up in your console log if the client has been directed to execute JSON.parse() on a string beginning with u instead of a stringified object. "u" is typically a stringified version of the undefined primitive.

Unexpected token u in JSON at position 0 error in console browser.
An example of the error.

This resource will show you how to fix the "Unexpected token u in JSON at position 0" error and is updated for 2024.

If you run the following code in your console browser, you'll get the SyntaxError.

JSON.parse(undefined)

When parsed, undefined is converted into u, which is then defined as the token in the error message of "Unexpected token u in JSON at position 0".

The Cause

This error is caused when you are attempting to parse a non-existent property. Typically, this is due to a misspelling, or simply referencing the wrong property (one that is not parsable).

my.data = '{"some":"data"}'; JSON.parse(my.date)

Notice the error? my.data is what we're trying to parse, but due to a typo, we're referencing a non-existent property, my.date, which is undefined. That means that there's an "unexpected token u in JSON at position 0".

Another less common cause of this error is not even receiving the JSON. This might be caused by a client side script that ignores errors and sends requests when they shouldn't. As there's no data to send to the request, you'll get an error each time this occurs.

The third cause of this error may be attempting to use JSON.parse() on data that contains an emoji.

The Fix(s)

From the causes, we can see that the fix for the Syntax Error "Unexpected token u in JSON at position 0" is relatively simple.

Ensure that you're pulling data from the right place. If the data does not exist, then the error will occur.

Ensure that the data is a valid JSON. If it is not, the error will occur. You can check the validity and formatting by using a tool like: https://jsonformatter.curiousconcept.com/.

Remove white space, which may corrupt your data. Here's an example:

data = data.trim(); // remove the unwanted whitespace theOutput = JSON.parse(data);

If you are using localstorage to store your data, clear it (browser console -> localStorage.clear()) and try again (remember to rewrite your data if this was done manually).

If you're using an emoji in the data, the simplest fix is to remove it. But if you must use an emoji, there are language-specific methods of encoding and decoding emojis to insure that they work with JSON.parse.

You may also want to delay receiving your data until the page or window is loaded by using window.onload (or with jQuery, $(document).ready()).

We hope this article helped you fix the "Unexpected token u in JSON at position 0" error. If you have any other questions, thoughts, or helpful tips around this topic, feel free to comment them below.

The Isotropic Codex is a collection of code snippets and education for WordPress, web and WooCommerce developers.
Subscribe & Share
If you liked this content, subscribe for our monthly roundup of WordPress news, website inspiration, exclusive deals and interesting articles.
Unsubscribe at any time. We do not spam and will never sell or share your email.
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Article By
James LePage
Contributors/Editors
notloggedin
James LePage is the founder of Isotropic, a WordPress education company and digital agency. He is also the founder of CodeWP.ai, a venture backed startup bringing AI to WordPress creators.
We're looking for new authors. Explore Isotropic Jobs.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram