-1

JSON structure

{
  "error": "RecordInvalid",
  "description": "Record validation errors",
  "details": {
  "email": [
   {
     "description": "Email: mark.vaugh@gmail.com is already being used by another user",
     "error": "DuplicateValue"
   }
 ],
 "name": [
  {
    "description": "Name: is too short (minimum one character)",
    "error": "ValueTooShort"
  }
 ]
 }
 }

Property names "details", "details:email", and "details:name" are dynamic(see screenshot) enter image description here

Here are the POCO classes:

public class ZendeskError
{
    [JsonProperty("details")]
    public Dictionary<string, List<ErrorKeyValue>> ErrorDetails { get; set; }

    [JsonProperty("description")]
    public string ErrorDescription { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }
}
public class ErrorKeyValue
{
    public KeyValuePair<string, List<PropertyFailureInformation>> PropertyError { get; set; }

}

public class PropertyFailureInformation
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }
}

Everything works well except the binding to class PropertyFailureInformation - see screenshot. enter image description here

Please advise where I am going wrong?

shobhit vaish
  • 951
  • 8
  • 22
  • Possible duplicate of [How can I deserialize a JSON object with dynamic property names?](https://stackoverflow.com/questions/24588347/how-can-i-deserialize-a-json-object-with-dynamic-property-names) – Ňɏssa Pøngjǣrdenlarp Aug 18 '19 at 14:57
  • 1
    Shouldn't it just be `public Dictionary> ErrorDetails { get; set; }`? I don't see why you need the extra `KeyValuePair>`, but maybe I'm missing something. – canton7 Aug 18 '19 at 14:59
  • @canton7 You are absolutely right - Thanks! That worked and silly me. – shobhit vaish Aug 18 '19 at 15:05

1 Answers1

2

You don't need ErrorKeyValue. ErrorDetails should just be:

public Dictionary<string, List<PropertyFailureInformation>> ErrorDetails { get; set; }

That is:

public class ZendeskError
{
    [JsonProperty("details")]
    public Dictionary<string, List<PropertyFailureInformation>> ErrorDetails { get; set; }

    [JsonProperty("description")]
    public string ErrorDescription { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }
}

public class PropertyFailureInformation
{
    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("error")]
    public string Error { get; set; }
}

See DotNetFiddle

canton7
  • 37,633
  • 3
  • 64
  • 77