JSON.parse, Unexpected token T in JSON at position 0

11,041

What is happening and why

When you reach the Rate limit set by the remote website, it returns the string "Too many requests. Please try again later."

As you could tell, this isn't valid JSON. If you try to JSON.parse() this string, you will get an error thrown.

let str = "Too many requests. Please try again later.";

console.log(JSON.parse(str));

Instead, you should do two things:

Read response.statusCode

First, check which status code is sent on successful requests. Let's say it is 200. You can then add a block that handles status codes that are different from 200:

Request.get(url, (error, response, body) => {
    if(error) {
        return console.dir(error);
    }
    if(response.statusCode != 200){ //200 is the status code on successful requests in this case
        return console.log(response.statusCode+" "+response.body);
    }
    var object = JSON.parse(body);
});

From there, you can even check which status code is sent on "Too many requests" responses. Let's assume it is 429, as specified by RFC 6585.

Request.get(url, (error, response, body) => {
    if(error) {
        return console.dir(error);
    }
    if(response.statusCode != 200){
        if(response.statusCode == 429)
            return console.log("429 Too many requests"); //429 error
        else
            return console.log(response.statusCode+" "+response.body); //some other status code that isn't '200 OK'
    }
    var object = JSON.parse(body);
});

Add a try-catch block around JSON.parse()

JSON.parse() is a function that will return the JSON object from a String if it is properly formatted, or throw an error otherwise.

It is thus best practices to wrap it in a try-catch block if the string is coming from another website, as you can't tell that the string will be properly formatted in 100% of cases.

Your final code and its error handling, for me, would look like this in the end:

Request.get(url, (error, response, body) => {
    if(error) {
        return console.dir(error);
    }
    if(response.statusCode != 200){
        if(response.statusCode == 429)
            return console.log("429 Too many requests");
        else
            return console.log(response.statusCode+" "+response.body);
    }
    var object;
    try{
        object = JSON.parse(body);
    }catch(err){
        console.log("Error on parsing string", err);
    }
});
Share:
11,041
quo91
Author by

quo91

Casual dude that sometimes tries to make his code work :D

Updated on June 04, 2022

Comments

  • quo91
    quo91 almost 2 years

    So I have a little web socket program that pulls data from a website. Sometimes it reaches the maximum of 6 queries per seconds and when it does it crashes. I put an if statement to check if the request was received but it doesn't seem to work. The part of the code making all this mayhem goes like this:

    Request.get(url, (error, response, body) => {
                    if(error) {
                        var flag = 1;
                        return console.dir(error);
                    }
                    if(flag == 1)
                    {
                        return;
                    }
    
                    var object = JSON.parse(body);  <-- error points here
                    if(!object || !object.data || !object.data.items || typeof object.data.items[0] === 'undefined')
                    {
                        return;
                    } ...
    

    The output error says this:

    undefined:1
    Too many requests. Please try again later.

    SyntaxError: Unexpected token T in JSON at position 0

    at JSON.parse ()
    at Request.get [as _callback] (/home/pi/Desktop/BitBot/test.js:702:23)
    at Request.self.callback (/home/pi/Desktop/BitBot/node_modules/request/request.js:185:22)
    at Request.emit (events.js:182:13) at Request. (/home/pi/Desktop/BitBot/node_modules/request/request.js:1161:10) at Request.emit (events.js:182:13) at IncomingMessage. (/home/pi/Desktop/BitBot/node_modules/request/request.js:1083:12) at Object.onceWrapper (events.js:273:13) at IncomingMessage.emit (events.js:187:15) at endReadableNT (_stream_readable.js:1094:12)

    Any ideas how to fix this?

  • quo91
    quo91 about 5 years
    Hmmm kinda works, but now I get this error: /home/pi/Desktop/BitBot/test.js:697 return console.log(response.statusCode+" "+response.body); ^ TypeError: Cannot read property 'statusCode' of undefined at Request.get [as _callback] (/home/pi/Desktop/BitBot/test.js:697:34) at self.callback...
  • Azami
    Azami about 5 years
    You most likely did a typo somewhere, if you call (error, response, body) and there is no internal error, there has to be a response.statusCode.