How to fix Unexpected Token in JSON error (for web developers)

preview_player
Показать описание
** Not a programmer? Read this! ** - If you get this error in an app you didn't make, and/or you're not a web developer... this video will not help you. You can try turning off adblockers (sometimes that helps), but if it's still broken, report it to the creator.

Getting this error in your code?

Unhandled Rejection (SyntaxError): Unexpected token in JSON at position 0

The position and the character may vary, but it's all caused by the same thing. Here's how to fix it.

This happens when you make a request to the server and parse the response as JSON, but it’s not JSON.

Рекомендации по теме
Комментарии
Автор

YES YES YES THANK
I've been stuck on this error for almost a week with no progress. It was for a different reason, my error was in the Java Spring back-end code, but your debugging method helped so much. I didn't know where that "json token" was coming from.
Thank youuu!!!!

randiir
Автор

So, in case anyone else ends up here with the same issue as me, with an unexpected token of "o", this is because you are trying to parse something that is already parsed somehow! The o is because JS is taking the object and running a toString method and giving you back "object". I am embarrassed to say this took me a number of hours to figure out. I miss strongly typed languages!! D:

catbat
Автор

Thank you so much for making this video!!! Helped me a lot for my final project in bootcamp :D

xilareyna
Автор

It's not even mentioned in Stackoverflow. Thanks for resolving my issue.

RockmanR
Автор

I can't say how much I'm thankful for you for this video.

adithyagowda
Автор

Thank you, sir. for helping me. I'm trying to fix this issue from 3 days before watching this

siddhant
Автор

You, sir, are my hero of the day. I thank you for setting me on the path to fixing my issue.

myklelange
Автор

Yoo broo, I can't say thank you enough. This problem has disturbed me for hours on end,

nickleydeonyango
Автор

Thank you - this helped solve my issue in my VUE project. It turns out I was fetching a path defined in my configuration/environment file, but I specified the name of the file as 'test.json' rather than preceding it with a slash: '/test.json' so it wasn't finding it properly. What was odd, is this fetch() call was being performed on my main VUE instance, so it should have actually been failing for EVERY route - but it was only failing for certain routes, which was odd.

zzZKnightZzz
Автор

very clear explanation, thanks Dave, good job 👍

alexyao
Автор

THANKS A LOT!
I was scratching my head wtf is going on, why I have everything what dude have but it's getting me this error and no data info....
And then realized that default url for React app is "/public", so typing "/numbers.json" in fetch is not going to work even if the file is in the same folder what my js file that have fetch in it...

WarframeCrunch
Автор

It can be really handy to enclose your JSON.parse clause in try/catch block and provide a fallback in a case when parsing fails with an error in order to prevent your app from crashing.

artemnakhodkin
Автор

Thank you so much I recently upgraded to nuxt 3 release candidate 3 and was getting this error from a graph call randomly but now I’m going to try .text to see what im really getting back

LuisPerez-tvmr
Автор

Thank you! I was unable to see my network request in debugger tool somehow but the .text really helped!

hugohouyez
Автор

Thanks for the explanation, it was brilliant. you know your stuff.

yusufdadkhah
Автор

Thanks a lot man, you explained so well and I could understand my error through your example.

felipegoncalves
Автор

Thank you so much! I love the explaination. understand my error more clearly and know how to avoid it in the future! ❤

karol.tormikoski
Автор

this video helped me to pinpoint the cause of error I was facing. Thanks!!!

NikhilSharma-fwpc
Автор

Hi! I am not a coder or programmer but I have a long code that I entered onto a webpage in order to retrieve an online wallet and even though I entered the code that I have printed on paper character for character verbatim, I keep getting hit with a “SyntaxError: Unexpected token, in JSON at position 701”. This is causing me a great deal in stress and any help would be greatly appreciated. Thank you. 🙏🏼

MBS-mptj
Автор

How to change the source code and run it (directly) and then get a build from the changed source code or make a build of the project from the wazuh-dashborad project

aljabitech