Search code examples
c#asp.net-mvcazure-cosmosdb

How to handle a array of Json object in Cosmos db using C#


I am working on an API which just add a record in a document which has the below JSON body.

My problem is the Status always shows null in CosmosDB.

I am sure the I am not handling the class definition for "Status" correctly. Status is an array of objects. Can anyone explain what I am doing wrong?

Json body:

{
    "name": "John", 
    "Status": [
        {
            "Status": "Closed",
            "StatusUpdatedDateTime": "2023-10-01T00:00:00Z",
            "StatusUpdatedBy": "XXX"
        }
    ]
}

Class definitions - TopObject class :

public class TopObject
{
     public string name { get; set; }
     public List<Status> status { get; set; }
}

public class Status
{
    public string Status { get; set; }  
    public DateTimeOffset StatusUpdatedDateTime { get; set; }
    public string StatusUpdatedBy { get; set; }
}

Solution

  • I agree with @madreflection, You can't have a class member with the same name as the class. Use [JsonProperty("Status")] and give the property any name other than "Status" (lowercase "status" was fine, albeit not the usual casing for a property). You can use define the classes as below:

    public class TopObject
    {
        [JsonProperty("name")]
        public string Name { get; set; }
    
        [JsonProperty("Status")]
        public List<Status> Status { get; set; }
    }
    public class Status
    {
        [JsonProperty("Status")]
        public string status { get; set; }
    
        [JsonProperty("StatusUpdatedDateTime")]
        public DateTimeOffset StatusUpdatedDateTime { get; set; }
    
        [JsonProperty("StatusUpdatedBy")]
        public string StatusUpdatedBy { get; set; }
    }
    

    Then you will be able to read the data successfully as shown below:

    enter image description here

    Here is the complete code for your reference:

    using Microsoft.Azure.Cosmos;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
    public class TopObject
    {
        [JsonProperty("name")]
        public string Name { get; set; }
    
        [JsonProperty("Status")]
        public List<Status> Status { get; set; }
    }
    public class Status
    {
        [JsonProperty("Status")]
        public string status { get; set; }
    
        [JsonProperty("StatusUpdatedDateTime")]
        public DateTimeOffset StatusUpdatedDateTime { get; set; }
    
        [JsonProperty("StatusUpdatedBy")]
        public string StatusUpdatedBy { get; set; }
    }
    
    public class Program
    {
        private static CosmosClient cosmosClient;
        private static Container container;
        private static string endpointUri = "<endpoint>";
        private static string primaryKey = "<primaryKey>";
        private static string databaseId = "<dbName>";
        private static string containerId = "<containerName>";
    
        public static async Task Main(string[] args)
        {
            cosmosClient = new CosmosClient(endpointUri, primaryKey);
            container = cosmosClient.GetContainer(databaseId, containerId);
            string documentId = "<id>";
            var topObject = await GetDocumentByIdAsync(documentId);
            Console.WriteLine("Name: " + topObject.Name);
            foreach (var status in topObject.Status)
            {
                Console.WriteLine("Status: " + status.status);
                Console.WriteLine("Updated Date: " + status.StatusUpdatedDateTime);
                Console.WriteLine("Updated By: " + status.StatusUpdatedBy);
            }
    
        }
        public static async Task<TopObject> GetDocumentByIdAsync(string documentId)
        {
            try
            {
                ItemResponse<TopObject> response = await container.ReadItemAsync<TopObject>(documentId, new PartitionKey(documentId));
                return response.Resource;
            }
            catch (CosmosException ex)
            {
                Console.WriteLine($"Error: {ex.StatusCode}, {ex.Message}");
                return null;
            }
        }
    }