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?
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> <strong>Status: " + $streamstatus2 + "</strong><br><br>" + $currentgame);
});
};
})
});
json.stream.game, provided your screenshot shows the result of loggingjsonstream_typeand it has a value oflive. Maybe that is also an indicator to whether a stream is online or not?