0

I have an API which expected to return result as below (which holds the data fetched from Cosmos DB)

public class ConsumerResponse
    {
        public string ObjectId { get; set; }
        public string ConsumerType { get; set; }
        public object Data { get; set; }
    }

I am using postman to test this API. Below is the API function which returns CustomerResponse object

[HttpGet]
    [Route("{container}/{consumerType}/{objectId}")]
    [ProducesResponseType(typeof(ConsumerResponse), (int)HttpStatusCode.OK)]
    [ProducesResponseType(typeof(ErrorResponse), (int)HttpStatusCode.BadRequest)]
    [ProducesResponseType(typeof(ErrorResponse), (int)HttpStatusCode.InternalServerError)]
    public async Task<ConsumerResponse> Get(string container , string consumerType, string objectId)
    {
        var response =  await _messageProcessingService.GetAsync(container, consumerType, objectId);
        return response;
    }

When I debug the code, I can see all the valid data mapped to each propery in CustomerResponse class. The "Data" is nested object. However when I call this API in Postman the nested values of "Data" object becomes empty as below

{
    "objectId": "8bea9894-8685-46e4-8637-5614e624b05e",
    "consumerType": "Patient",
    "data": {
        "name": [
            [
                []
            ],
            [
                []
            ],
            [
                []
            ],
            [
                []
            ]
        ],
        "gender": [],
        "birthDate": [],
        "countryCode": [],
        "isActive": [],
        "consumerConsent": [],
        "accountStatus": [
            [
                [
                    []
                ]
            ]
        ]
    }
}

However if I change the data type of "Data" as string then I am getting result as below

{
    "objectId": "8bea9894-8685-46e4-8637-5614e624b05e",
    "consumerType": "Patient",
    "data": "{\r\n  \"name\": {\r\n    \"use\": \"Official\",\r\n    \"surname\": \"Dope\",\r\n    \"firstName\": \"Johny\",\r\n    \"prefix\": \"Mr.\"\r\n  },\r\n  \"gender\": \"Male\",\r\n  \"birthDate\": \"1991-02-02\",\r\n  \"countryCode\": \"IN\",\r\n  \"isActive\": false,\r\n  \"consumerConsent\": true,\r\n  \"accountStatus\": [\r\n    {\r\n      \"startDate\": \"2021-09-08T09:27:21.1946786+10:00\"\r\n    }\r\n  ]\r\n}"
}

How do I get the values for nested structure when I use Object data type?

-Alan-

3
  • Not a lot to work with here. Also, the whole object thing you have going on with public object Data is very suspicious Commented Sep 16, 2021 at 0:40
  • can you change the Object to the Class of that type object and try? Commented Sep 16, 2021 at 3:56
  • So, how about just using string rather than object? It seems to be a data convert issue, and there aren't many choices to try here. Commented Sep 16, 2021 at 7:44

1 Answer 1

0

You can get all values as the data type as string because it gets the key value pair so you can check here to convert to json check here

In a Azure Function you need to add a reference to NewtonSoft.JSON

Newtonsoft.Json library is a special case. You can include it by adding this at the top of the script file:

#r "Newtonsoft.Json"

using Newtonsoft.Json;

and add the below lines of code to get the json nested values.

var response = await client.GetAsync("<url>");

var json = await response.Content.ReadAsStringAsync();

var obj= JsonConvert.DeserializeObject<"Type">(json);

Refer here for more info link 1 & link 2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.