1

How i can access below object type value which is coming as request body from data factory output of another function app in http trigger function. Now i need to perform some operation with these ouput in http trigger function. { "functionName": "GoogleAuth", "method": "POST", "headers": {}, "body": { "Response": "[{"id":"hjk","name":"abc","description":"hki","brand":"Birds Eye","ean":"125","mediaStorageKey":"124","maxQuantity":6,"price":1.75,"size":224.0,"sizeUnits":"Grams"}]", "effectiveIntegrationRuntime": "DefaultIntegrationRuntime (West Europe)", "executionDuration": 0, "durationInQueue": { "integrationRuntimeQueue": 0 }, "billingReference": { "activityType": "ExternalActivity", "billableDuration": [ { "meterType": "AzureIR", "duration": 0.016666666666666666, "unit": "Hours" } ] } } }

I am trying to access it like this but is showing error.

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic body = JsonConvert.DeserializeObject(requestBody);
dynamic data = body["Response"];
product.OfferId = string.IsNullOrEmpty(Convert.ToString(data[0]["id"])) ? " " :Convert.ToString(data[0]["id"]);

Error:Cannot access child value on Newtonsoft.Json.Linq.JValue.

shashank shekhar
  • 132
  • 2
  • 15
  • This request body is not a valid JSON – Chris Berlin Jun 03 '21 at 10:58
  • I cannot do anything for reqst body i am getting as these values are coming as object type from another function app output of data factory . – shashank shekhar Jun 03 '21 at 11:59
  • Can you show pls value of dynamic data = body["Response"]; – Serge Jun 03 '21 at 12:54
  • [{"id":"b","name":"Seriously Tasty Pasties Traditional","description":"Seriously Tasty Traditional Beef and Vegetable Pasty 199G","brand":"Seriously Tasty","ean":"6","mediaStorageKey":"7fb","maxQuantity":6,"price":0.55,"size":null,"sizeUnits":null}] – shashank shekhar Jun 03 '21 at 13:03

1 Answers1

0
"Response": "[{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]

First, please set a break point to check the requestBody value, because the above value is not a valid json. You could search "Json Parse Online" and use online tools to verify it.

After changing the response as below (remove the '"' before the '[' and after the ']'), your code works well

string requestBody = "{\"Response\": [{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]}";
dynamic body = Newtonsoft.Json.JsonConvert.DeserializeObject(requestBody);
dynamic  data = body["Response"];
                     
var  offerId = string.IsNullOrEmpty(Convert.ToString(data[0]["id"])) ? " " : Convert.ToString(data[0]["id"]); //output: cc4324be-fa0e-424c-97e9-97644752f609

Second, from your comment, the dynamic data value is:

[{"id":"b","name":"Seriously Tasty Pasties Traditional","description":"Seriously Tasty Traditional Beef and Vegetable Pasty 199G","brand":"Seriously Tasty","ean":"6","mediaStorageKey":"7fb","maxQuantity":6,"price":0.55,"size":null,"sizeUnits":null}] 

In this scenario, you can convert the data value to string, then, use JsonConvert.DeserializeObject() convert the string value, after that, you could get the id value. code like this:

string requestBody = "{\"Response\": [{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]}";
dynamic body = Newtonsoft.Json.JsonConvert.DeserializeObject(requestBody);
dynamic  data = body["Response"];
                     
var  offerId = string.IsNullOrEmpty(Convert.ToString(data[0]["id"])) ? " " : Convert.ToString(data[0]["id"]); //output: cc4324be-fa0e-424c-97e9-97644752f609

var datastr = Convert.ToString(data);

var datavalue = "[{\"id\":\"cc4324be-fa0e-424c-97e9-97644752f609\",\"name\":\"Seriously Tasty Pasties Traditional\",\"description\":\"Seriously Tasty Traditional Beef and Vegetable Pasty 199G\",\"brand\":\"Seriously Tasty\",\"ean\":\"5011187110319\",\"mediaStorageKey\":\"https://rt-1-dv-euw-retailapis-end-01.azureedge.net/5011187110319_1_1024x1024_20210325.png\",\"maxQuantity\":6,\"price\":0.55,\"size\":null,\"sizeUnits\":null}]";
dynamic databody = Newtonsoft.Json.JsonConvert.DeserializeObject(datastr); //for test purpose, change datastr to datavalue.

var Id = string.IsNullOrEmpty(Convert.ToString(databody[0]["id"])) ? " " : Convert.ToString(databody[0]["id"]); //output: cc4324be-fa0e-424c-97e9-97644752f609

The debug screenshot as below:

enter image description here

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Thanks ..I have pasted(edited) full req body which i am getting from data factory outpout of another function app.And it is a valid json if you will put all in json validatory.Any suggestion. – shashank shekhar Jun 04 '21 at 14:32
  • 1
    Hi @shashankshekhar, Please check [this screenshot](https://i.stack.imgur.com/VdDkj.gif), it seems that the edited request body still has some error, after changing the Response's value from string to an array, the issue disappears. Might be you can't change the response body, if that is the case, please refer the second method in my reply, after you are getting the response body, you could convert it to string (the result should be like the datavalue's value), then, use JsonConvert.DeserializeObject() convert the string value, after that, you could get the id value. – Zhi Lv Jun 08 '21 at 06:58