Search code examples
c#.net-coresendgriddotnet-httpclientjson-deserialization

How to deserialize dictionary types to json in dotnet core


I am working on some integrations with SendGrid's APIs, particularly around the search functionality in emails.

The API response is like so:

{
   "result":{
      "personsemail@domain.com":{
         "contact":{
            "address_line_1":"",
            "address_line_2":"",
            "alternate_emails":[
               
            ],
            "city":"",
            "country":"",
            "email":"personsemail@domain.com",
            "first_name":"Bob",
            "id":"111111-1111-1111-1111-111111111111",
            ...
         }
      },
      "anotheremail@domain.com":{
         "contact":{
            "address_line_1":"",
            "address_line_2":"",
            "alternate_emails":[
               
            ],
            "city":"",
            "country":"",
            "email":"anotheremail@domain.com",
            "first_name":"Alice",
            "id":"111112-1112-1112-1112-111111111112",
            ...
         }
      }
      ...
   }
}

Does anyone know how I can deserialize this appropriately so I can use it like a normal list to iterate/search with? I'm hoping I can use something like this:

var recipients = (await searchResponse.Content.ReadFromJsonAsync<**my-poco**>());

Which is similar to what I'm using for all the other calls - but I'm not sure how to create the POCO that will allow for a simple deserialization. I'm happy to ignore the top level personsemail@domain.com entry and just search through the results with linq..

Any suggestions would be appreciated!


Solution

  • I ended up getting this working with the following class. This is a variation to Peter's answer:

    public class SendGridContactSearchResponse
    {
        [JsonProperty("result", NullValueHandling = NullValueHandling.Ignore)]
        //public Result Result { get; set; }
        public Dictionary<string, Contact> result { get; set; }
    }
    
    public partial class Contact
    {
        [JsonProperty("contact", NullValueHandling = NullValueHandling.Ignore)]
        public contact contact { get; set; }
    }
    
    public partial class contact
    {
        [JsonProperty("address_line_1", NullValueHandling = NullValueHandling.Ignore)]
        public string AddressLine1 { get; set; }
    
        [JsonProperty("address_line_2", NullValueHandling = NullValueHandling.Ignore)]
        public string AddressLine2 { get; set; }
    
        [JsonProperty("alternate_emails", NullValueHandling = NullValueHandling.Ignore)]
        public List<object> AlternateEmails { get; set; }
    
        [JsonProperty("city", NullValueHandling = NullValueHandling.Ignore)]
        public string City { get; set; }
    
        [JsonProperty("country", NullValueHandling = NullValueHandling.Ignore)]
        public string Country { get; set; }
    
        [JsonProperty("email", NullValueHandling = NullValueHandling.Ignore)]
        public string Email { get; set; }
    
        [JsonProperty("first_name", NullValueHandling = NullValueHandling.Ignore)]
        public string FirstName { get; set; }
    
        [JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
        public Guid? Id { get; set; }
    
        [JsonProperty("last_name", NullValueHandling = NullValueHandling.Ignore)]
        public string LastName { get; set; }
    
        [JsonProperty("list_ids", NullValueHandling = NullValueHandling.Ignore)]
        public List<object> ListIds { get; set; }
    
        [JsonProperty("segment_ids", NullValueHandling = NullValueHandling.Ignore)]
        public List<object> SegmentIds { get; set; }
    
        [JsonProperty("postal_code", NullValueHandling = NullValueHandling.Ignore)]
        public string PostalCode { get; set; }
    
        [JsonProperty("state_province_region", NullValueHandling = NullValueHandling.Ignore)]
        public string StateProvinceRegion { get; set; }
    
        [JsonProperty("phone_number", NullValueHandling = NullValueHandling.Ignore)]
        public string PhoneNumber { get; set; }
    
        [JsonProperty("whatsapp", NullValueHandling = NullValueHandling.Ignore)]
        public string Whatsapp { get; set; }
    
        [JsonProperty("line", NullValueHandling = NullValueHandling.Ignore)]
        public string Line { get; set; }
    
        [JsonProperty("facebook", NullValueHandling = NullValueHandling.Ignore)]
        public string Facebook { get; set; }
    
        [JsonProperty("unique_name", NullValueHandling = NullValueHandling.Ignore)]
        public string UniqueName { get; set; }
    
        [JsonProperty("custom_fields", NullValueHandling = NullValueHandling.Ignore)]
        public CustomFields CustomFields { get; set; }
    
        [JsonProperty("created_at", NullValueHandling = NullValueHandling.Ignore)]
        public DateTimeOffset? CreatedAt { get; set; }
    
        [JsonProperty("updated_at", NullValueHandling = NullValueHandling.Ignore)]
        public DateTimeOffset? UpdatedAt { get; set; }
    
        [JsonProperty("_metadata", NullValueHandling = NullValueHandling.Ignore)]
        public Metadata Metadata { get; set; }
    }
    

    The fix was in the SendGridContactSearchResponse class referencing a Contact class in the dictionary. The invocation was this:

    var searchResponse = JsonConvert.DeserializeObject<SendGridContactSearchResponse>(json);
    

    and the deserialization worked fine!