Tutorial: Working with Contentstack – Mapping data to generated models
When setting up a basic Contentstack POC I began to follow the documentation in the SDK:
https://www.contentstack.com/docs/developers/dot-net/get-started-with-dot-net-sdk/
https://www.contentstack.com/docs/developers/dot-net/api-reference/api/
Below is an example snippet from the documentation:
Query query = client.ContentType("blog").Query();
query.Where("title", "welcome");
query.IncludeSchema();
query.IncludeCount();
query.Find<Blog>().ContinueWith((t) => {
if (!t.IsFaulted) {
ContentstackCollection<Blog> result = t.Result;
Console.WriteLine("result" + result);
}
});
In this example, data is mapped to the Blog object. These models can be generated when using the Contentstack model generator. Upon using this generator, a collection of models are added to the solution:

Rather than map to a Blog, I decided to get a Product and attempt to map data to the generated Product model. As per the documentation I set up a very basic example:
ContentstackClient client = new ContentstackClient(contentStackConfig.StackApiKey, contentStackConfig.DeliveryToken, "production");
Query query = client.ContentType("product").Query();
var queryResult = await query.Find<Product>();
However, when mapping to the generated Product model I experienced two errors:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘ContentstackModels.Models.Brand’ 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 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.
Error 2:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘ContentstackModels.Models.Bank’ 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 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.
It appears that the models generated in v0.2.3 of the Generator are not compatible with the JSON object that is returned from the API calls, as the properties are not correctly set to a collection. I therefore had to update two properties to include a collection. First, the Brand property on the Product model was updated from:
public Brand Brand { get; set; }
To:
public List<Brand> Brand { get; set; }
And as Products contain a list of bank offers, I had to also update the following property on the GroupProductBankOffers model:
public Bank Bank { get; set; }
Change to:
public List<Bank> Bank { get; set; }
Data should now map to your Products class. I assume these errors will be present in other generated models until an update is made to the generator.
Neil Shack
Hi Neil,
This is Dhaval from Contentstack. Thanks a lot for identifying the issues and suggesting a workaround to the users. Really appreciate it.
We have updated the model generator, and it should now work perfectly for anyone using it.
Thanks again. Please feel free to contact us at support@contentstack.com if you have any questions or suggestions.