I need help with converting JSON data from HubSpot’s Contact API into C# objects. I’m working with RestSharp and Newtonsoft.Json but can’t figure out the right class structure for deserialization.
I can get the basic ID values but when I try to access the nested attributes section everything breaks. What’s the correct way to set up my C# classes for this?
Honestly, just use JObject.Parse() first to debug what you’re actually getting. I spent hours on the same issue - turns out the API response was structured differently than the docs showed. Once you see the real JSON structure with jObject[“customers”][0] and all that, you can build your classes properly.
the nested attributes structure is tricky bc each property has that “value” wrapper. try creating a generic AttributeValue class with a Value property, then use it like public AttributeValue<string> name { get; set; } in ur attributes class. worked for me with similar HubSpot APIs.
Hit this same problem last month with HubSpot contacts. The trick is handling those dynamic attribute names correctly. Here’s what worked for me: create separate classes for each nested level - Contact class, Attributes class with properties for your expected fields, and Detail class for profiles. Don’t try making the attributes generic right away. Just map the specific fields you need (Name, Surname, Organization) with their own classes that have a Value property. The profiles array gets messy, so make your Details property a List where ContactDetail handles kind/data/primary fields. Also - make sure your root class uses exact property names (“customers” not “contacts”) and handle pagination fields correctly.
Use JsonProperty attributes for those property names with dashes. The account-id field needs [JsonProperty("account-id")] - C# hates hyphens in property names. For attributes, I just deserialize to a Dictionary<string, AttributeWrapper> where AttributeWrapper has a string Value property. Then access them like contact.Attributes["name"].Value. Watch the timestamp fields - they’re Unix timestamps in milliseconds, so convert them if you want DateTime objects. The profiles array can be null depending on the contact data.