How to extract data from Twitch streaming API response

I’m working on a project where I need to update button colors depending on whether a streamer is currently broadcasting. I can successfully fetch the JSON data from the API and store it in a variable, but I’m stuck on the next step.

From what I understand, I need to examine the “stream” field in the JSON response - when it’s null, the streamer is offline. However, I’m not sure how to properly check this condition in my code.

Here’s what I have so far for the button click event that should update the stream status:

private void UpdateStatus_Click(object sender, RoutedEventArgs e)
{
    string apiUrl = @"https://api.twitch.tv/kraken/streams/somestreamer?client_id=abc123xyz";
    
    var jsonResponse = new WebClient().DownloadString(apiUrl);
    
    StreamData data = JsonConvert.DeserializeObject<StreamData>(jsonResponse);
    Console.WriteLine(data.stream);
    if data.stream.title == "Call of Duty"
    {
        streamButton.Background = Brushes.Green;
    }
}

The JSON parsing works fine and I can access the stream data, but I can’t figure out the right way to determine if the stream is active or not. Any suggestions on how to properly handle this check?

btw you’re missing parentheses in that if statement - should be if (data.stream != null) not if data.stream.title. also wrap that webclient call in try-catch since api calls can fail and crash your app.

Just add a null check: if (data.stream != null) { ... } before you check the title. When the stream’s offline, it’s null, so accessing .title throws an error. Wrap that check and you’re set!

Your if statement’s missing parentheses and will throw a syntax error. Change if data.stream.title == "Call of Duty" to if (data.stream != null && data.stream.title == "Call of Duty"). You’ve got to check if the stream object exists before trying to access its properties. If you just want to see if someone’s live, use if (data.stream != null) - null stream means they’re offline. BTW, the Kraken API is deprecated. You should switch to the Helix API eventually since Twitch will kill Kraken support at some point.

I’ve worked with similar streaming setups and there’s another angle to consider. Don’t just check for null values - you need to handle empty API responses too, plus cases where the stream object exists but has no real data.

I’ve seen streams where the field isn’t null but the streamer’s actually offline due to connection problems or API lag. Try adding a secondary check on stream.viewers or stream.created_at to make sure the data’s actually current.

Also, wrap some error handling around that WebClient call - network timeouts happen all the time with external APIs. But yeah, fix that syntax error first like others said. That missing parenthesis will kill your compilation completely.

Your code has a basic problem - you’re checking for a specific game title to see if someone’s streaming. That won’t work since streamers switch games all the time. Just check if the stream object exists. When data.stream is null, they’re offline. When it has data, they’re live - doesn’t matter what game they’re playing. Use if (data.stream != null) { streamButton.Background = Brushes.Green; } else { streamButton.Background = Brushes.Red; }. Way more reliable than trying to match game titles that constantly change.