0

I need to count data from reqst body in httptrigger function.Data is coming as object type. I am deserialisizing the reqd body as shown below.Below are the object type data i am getting in req.body.

{
  "Response": [
    {
      "id": "1",
      "name": "Warburtons Medium Sliced Soft White Bread 400g",
      "description": "Warburtons Medium Sliced White 400G",
      "brand": "Warburtons",
      "ean": "123",
      "mediaStorageKey": "b",
      "maxQuantity": 6,
      "price": 0.95,
      "size": 400,
      "sizeUnits": "Grams"
    },
    {
      "id": "a",
      "name": "Co-op Orvieto Classico 75cl",
      "description": "Co-op Orvieto Classico 75CL",
      "brand": "Co-op",
      "ean": "489",
      "mediaStorageKey": "c",
      "maxQuantity": 6,
      "price": 5.5,
      "size": 75,
      "sizeUnits": "Centilitres"
    },
    {
      "id": "kl",
      "name": "Co Op Garden Peas in Water 290g",
      "description": "Co-op Garden Peas 290G",
      "brand": "Co Op",
      "ean": "678",
      "mediaStorageKey": "f",
      "maxQuantity": 6,
      "price": 0.45,
      "size": 175,
      "sizeUnits": "Grams"
    }
  ]
}
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic body = JsonConvert.DeserializeObject(requestBody);
             
dynamic data = body["Response"];

Till this i am getting data like below.

{[{"id":"1","name":"b"},{"id":"f","name":"j"}]}

But now i am not able to count these data which gives 2 in this case as i have to apply for loop. Neither Count,Count() is working here. I am getting the below error.

Newtonsoft.Json.Linq.JValue does not contain a definition for Count

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
shashank shekhar
  • 132
  • 2
  • 15
  • Can you post the full `requestBody` JSON (at least enough that we can run your `dynamic data = body["Response"]`) and get something similar to what you show (which, by the way, is not valid JSON) – Flydog57 Jun 02 '21 at 19:22
  • Might you please [edit] your question to share a [mcve]? If I take the JSON in your question and try to parse it, I get an error *`Invalid property identifier character: [. Path '', line 1, position 1.`*, see https://dotnetfiddle.net/FVmqgM. The problem is that the JSON is malformed -- it has additional outer brackets `{}` that should not be present. – dbc Jun 02 '21 at 20:02
  • If I fix the JSON by removing the outer brackets, your code generates an *`Accessed JArray values with invalid key value: "Response". Int32 array index expected.` exception because the root JSON container is an array not an object, so there is no `"Response"` property. See https://dotnetfiddle.net/KYJuhG. – dbc Jun 02 '21 at 20:02
  • My wild guess is that the original JSON looks like `{"Response" : [{"id":"1","name":"b"},{"id":"f","name":"j"}]}`, that when he gets the value for response, it becomes `[{"id":"1","name":"b"},{"id":"f","name":"j"}]` and that the debugger shows it with `{}` brackets around it. Just a guess though – Flydog57 Jun 02 '21 at 20:28
  • Your wild guess seems reasonable but a [mcve] would clarify. Especially since question is using `dynamic` for everything which makes it hard to know what is happening by inspection (and possibly is causing the problem). – dbc Jun 02 '21 at 21:42
  • Hi @shashank shekhar, it works well in my azure http trigger if I use the code:`var count = data.Count;`. It could get the result `2`. What is your version of asp.net core? Also what's the data you post ,could you please share with us? – Rena Jun 03 '21 at 04:19
  • so this is my actual structure in body .I am using sample data but with actual structure.My version is .net core 3.1.I have edited in question with actual structure i am getting in body – shashank shekhar Jun 03 '21 at 04:35

2 Answers2

1

create a class with these two fields id and name

public class Item {
  public Int Id {get; set;}
  public String Name {get; set;}
}

and specify the type for your deserialized value:

dynamic body = JsonConvert.DeserializeObject(requestBody);
var rows = JsonConvert.DeserializeObject<List<Item>>(body["Response"]);

now you can use rows.Count();

AlleXyS
  • 2,476
  • 2
  • 17
  • 37
  • It is throwing error. The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject>(string)' has some invalid arguments. – shashank shekhar Jun 03 '21 at 03:19
0

If you only need to know the amount of entries inside the Response collection then you can simply do that by making use of JObject and JArray:

string rawJson = ...;
JObject semiParsedJson = JObject.Parse(rawJson);
JArray entries = (JArray)semiParsedJson["Response"];
int count = entries.Count;
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • I tried as u suggested but it is showing error. string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic body = JsonConvert.DeserializeObject(requestBody); JObject semiParsedJson = JObject.Parsebody JArray entries = (JArray)semiParsedJson["Response"]; int count = entries.Count; Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JArray'. – shashank shekhar Jun 03 '21 at 08:16
  • @shashankshekhar That means the the copy-pasted response does not reflect to the reality. Could you please update? – Peter Csala Jun 03 '21 at 08:49
  • data is coming as request body from data factory output of another function app as object type – shashank shekhar Jun 03 '21 at 12:02
  • @shashankshekhar In the provided sample the `Response` is a collection (`[]`), not an object (`{}`). Maybe I have autocorrected something when I've edited your question. Could you please overwrite it with the content of the `requestBody` variable? – Peter Csala Jun 03 '21 at 12:11