Search code examples
jsondeserializationslack-apijson-deserializationasp.net-core-5.0

SlackAPI - payload deserialization with dynamic json properties


I work on my SlackAPI application.

I have created the modal window and when the form from this modal is submitted - I receive a JSON payload that looks like this (not important JSON part is removed)

{
   "type":"view_submission",
   "view":{
      "state":{
         "values":{
            "IGhn":{
               "e5+":{
                  "type":"static_select",
                  "selected_option":{
                     "text":{
                        "type":"plain_text",
                        "text":"2021\/8",
                        "emoji":true
                     },
                     "value":"2"
                  }
               }
            }
         }
      },

This payload is processed by my endpoint correctly - except the part - state - values, which are nulls...

I have the models defined like this:

public class View
{
...
[JsonProperty("state")]
public State State { get; set; }
...
}

public class State
{
[JsonProperty("values")]
public Dictionary<string, Values> Values { get; set; }
}

public class Values
{
[JsonProperty()] // <- This is the problem
public Dictionary<string, DynamicValue> something { get; set; }
}

public class DynamicValue
{
[JsonProperty("selectedOptions")] // <- This is null
public Dictionary<string, SelectedOption> SelectedOptions { get; set; }
}

If the values (property names) will be static it will be okay, but the problem is, that : IGhn / e5+ are dynamically changing - so the deserialization does not work...

Must say that the whole JSON is deserialized correctly, but I can't deserialize the rest in under IGhn (I even don't know how to create class for it...do the deserializer know what to do.... )


Solution

  • Ok - I found the solution:

    I have changed the class State:

        public class State
            {
                [JsonProperty("values")]
                public Dictionary<string, Dictionary<string, ParentForSelectedOption>> Values { get; set; }
            }
    

    Then Object ParentForSelectedOption is deserialized in a regular way - so these dynamic names were solved by the use of Dictionaries and these can be removed:

    public class Values
    {
    [JsonProperty()] // <- This is the problem
    public Dictionary<string, DynamicValue> something { get; set; }
    }
    
    public class DynamicValue
    {
    [JsonProperty("selectedOptions")] // <- This is null
    public Dictionary<string, SelectedOption> SelectedOptions { get; set; }
    }