Search code examples
javascriptjqueryjsontwitch

JSON - Cannot read property 'viewers' of undefined


I am trying to get a list of viewers from twitch and it keeps giving me an error even though I am pretty sure I have the right format ...

here is the code and here is an example on jsfiddle:

$(document).ready(function () {

    $.getJSON("http://tmi.twitch.tv/group/user/nightblue3/chatters?callback=?", function (data) {
        console.log(data.chatters.viewers); //This should be in the right format based of the json data?!
    });


});

Solution

  • You named the variable data, but that data object has another data object inside that contains the chatters.viewers, so it should be :

    $.getJSON("http://tmi.twitch.tv/group/user/nightblue3/chatters?callback=?", function (data) {
        console.log(data.data.chatters.viewers); //This should be in the right format based of the json data?!
    });
    

    Fiddle