How to extract _links data from Twitch API response?

I’m working on a C# application that calls the Twitch API and I’m having trouble accessing the _links section from the JSON response. When I make a request to get stream information, the response includes a _links object with properties like self and channel, but my current deserialization setup isn’t working properly.

Here’s what I’m trying:

HttpClient client = new HttpClient();
string jsonResponse = await client.GetStringAsync("https://api.twitch.tv/kraken/streams/somechannel");
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(StreamData));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonResponse));
StreamData data = (StreamData)serializer.ReadObject(stream);
stream.Close();

My data model looks like this:

[DataContract]
public class StreamData
{
    [DataMember]
    public Dictionary<string, string> _links { get; set; }
    
    [DataMember]
    public string self { get; set; }
    
    [DataMember]
    public string channel { get; set; }
    
    [DataMember]
    public StreamInfo stream { get; set; }
}

The self and channel properties always come back as null even though they exist in the _links section of the JSON. I tried using a Dictionary but it’s not working as expected. How can I properly deserialize the _links object to access its nested properties?

DataContractJsonSerializer gets weird with underscore properties. Had the same headache with APIs that don’t follow standard naming.

Your model expects _links to be strings, but the JSON actually has _links as an object with self and channel properties inside it. That’s your mismatch right there.

Ditch Dictionary<string, string> and build a proper nested structure like everyone else suggested. If you’re stuck with DataContractJsonSerializer, add the Name parameter to your DataMember attributes - it gives you better mapping control. This serializer’s pretty rigid about property matching compared to others.

Honestly? Switch to System.Text.Json instead. It handles this stuff way better and runs faster. JsonPropertyName works the same but without all the config mess. I switched on my last project and it killed so many API integration headaches.

newtonsoft.json is way easier than datacontract for this. just slap a jsonproperty attribute on it and it’ll handle underscore fields automatically. something like [JsonProperty("_links")] public LinksData Links { get; set; } - works great in my experience.

Been dealing with API headaches like this for years. Your Dictionary approach won’t work - you’re trying to map an object structure to key-value pairs.

The JSON has _links as an object with self and channel properties. Your model treats it like a flat dictionary. That’s why everything’s null.

Writing custom C# deserializers for every API quirk gets old fast. I stopped doing manual API integrations because they break constantly when APIs change.

I automate all my API workflows now instead of writing code. Set up a visual workflow that connects to Twitch API, automatically parses the JSON (including those underscore fields), and routes the data wherever you need it.

No serializers to debug. No model classes to maintain. The platform handles JSON parsing automatically and you can map nested fields with clicks instead of code. When Twitch changes their API structure, you update the workflow in minutes instead of recompiling.

I use this approach for all our production API integrations now. Way more reliable than custom deserialization code.

Your model structure’s wrong. The self and channel properties need to be inside the _links object, not at the root.

Here’s the fix:

[DataContract]
public class StreamData
{
    [DataMember]
    public LinksData _links { get; set; }
    
    [DataMember]
    public StreamInfo stream { get; set; }
}

[DataContract]
public class LinksData
{
    [DataMember]
    public string self { get; set; }
    
    [DataMember]
    public string channel { get; set; }
}

Access it with: data._links.self and data._links.channel.

But honestly? Manual API handling gets messy fast. I’ve automated API integrations for years and visual workflow tools work way better.

Skip all that C# code. Set up an automated workflow that pulls from Twitch API, grabs what you need from the JSON, and sends it wherever. Takes 10 minutes vs hours debugging deserialization.

The visual interface maps JSON fields directly - no data contracts or serializers. You get error handling, scheduling, and monitoring built in.

Your model structure’s wrong. The self and channel properties are nested inside the _links object in the JSON, not at the root. You need a separate class for the links data.

Try this:

[DataContract]
public class StreamData
{
    [DataMember(Name = "_links")]
    public LinksData Links { get; set; }
    
    [DataMember]
    public StreamInfo stream { get; set; }
}

[DataContract]
public class LinksData
{
    [DataMember]
    public string self { get; set; }
    
    [DataMember]
    public string channel { get; set; }
}

The Name = "_links" attribute maps the property correctly. After deserialization, access the links through data.Links.self and data.Links.channel. I’ve used this pattern with Twitch API responses before - works way better than trying to flatten everything into dictionaries.