Search code examples
json.netauth0restsharp

Cannot deserialize the current JSON array - Auth0 Management API Endpoint - despite using online POCO generators


I am working with Auth0 Management API endpoints, the issue with this one.

https://auth0.com/docs/api/management/v2?_ga=2.197148647.957265821.1601726170-1678439180.1588036522#!/Users/get_users

Here is my rest code.

    var client = new RestClient(tempapiendpoint);
    var request = new RestRequest(Method.GET);
    request.AddHeader(header, bearerstring);
    request.AddParameter(specificfieldname,specificfields);
    request.AddParameter(includefieldname, includetrueorfalse);
    IRestResponse response = await client.ExecuteAsync(request);
    var myDeserializedClass = JsonConvert.DeserializeObject<Root>(response.Content);

I have the following response.

[
  {
    "email": "somevalue",
    "name": "somevalue",
    "nickname": "somevalue",
    "user_id": "somevalue"
  },
  {
    "email": "somevalue",
    "name": "somevalue",
    "nickname": "somevalue",
    "user_id": "somevalue"
  },
  {
    "email": "somevalue",
    "name": "somevalue",
    "nickname": "somevalue",
    "user_id": "somevalue"
  }
]

At this point, I use an online class generator, such as, https://json2csharp.com/

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class MyArray    {
        [JsonProperty("email")]
        public string Email; 

        [JsonProperty("name")]
        public string Name; 

        [JsonProperty("nickname")]
        public string Nickname; 

        [JsonProperty("user_id")]
        public string UserId; 
    }

    public class Root    {
        [JsonProperty("MyArray")]
        public List<MyArray> MyArray; 
    }

and everytime, I get the same error.

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'RandomStuffGeneratorPrivate.POCO.Root' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

A few more things I have tried.

  • I have tried another source for class generation, https://app.quicktype.io/. I get the exact same error.
  • I have checked the json for validity. it is in the correct format.
  • even if hand map it, this is a straight forward json.
  • Further, I noticed that the name of the user class and the collection name are both the same. So, I changed the name for the user class. (it never gave any errors while debugging but I changed it anyway). no change in error
  • When generating classes, I am taking the json string directly from the live response during debugging, from IRestResponse response.content, just in case the online API documentation is making a mistake.
  • i have looked at other stack questions, and in those cases, I noticed that there was some mistake related to not having a list. Here, I definitely have mapped (the online generators wont make such a mistake) the returning array to a list.

Solution

  • Looks to me that you are deserializing to the wrong class, try using : JsonConvert.DeserializeObject<MyArray[]>(myJsonResponse)