0

I have a function that accesses the Twitch.tv API to retrieve various information about a live stream. When I try to retrieve the "game" key for a stream which is online it simply returns "undefined", although if I access the developer console I can see the logged json object includes the proper game key. How do I properly access the game key?

screenshot

My function:

$("#usernamelookupcheck").on("click", function() {
    $.getJSON("https://wind-bow.glitch.me/twitch-api/users/" + $("#usernamelookup").val(), function(json) {
        if (json["error"] == "Not Found") {
            $streamstatus2 = "User does not exist!";
            $("#lookupresult").html("User does not exist!");
        } else {
            $.getJSON("https://wind-bow.glitch.me/twitch-api/streams/" + $("#usernamelookup").val(), function(json) {
                $currentgame = json["game"];
                if (json["stream"] === null) {
                    $streamstatus2 = "Offline";
                } else {
                    $streamstatus2 = "Online" + "<br><br>";
                    console.log(json);
                };
                $("#lookupresult").html("<br><br> &nbsp; &nbsp; &nbsp; <strong>Status: " + $streamstatus2 + "</strong><br><br>" + $currentgame);

            });

        };
    })
});
3
  • It's json.stream.game, provided your screenshot shows the result of logging json Commented Jul 11, 2017 at 4:37
  • I noticed there is a property called stream_type and it has a value of live. Maybe that is also an indicator to whether a stream is online or not? Commented Jul 11, 2017 at 4:38
  • @SamuelToh That might be it, I might implement that in the near future to make the code more efficient. Commented Jul 11, 2017 at 4:49

1 Answer 1

1

you need to use

json['stream']['game'] or json.stream.game

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.