-2

I have below code and below Json Sample is returened in requestBody. I would like to check if my requestBody contains key ComputerModel or not, and if it contains I would like to get that value. How can I do that?

Json response I get in quick watch:(requestBody)

{
  "id": "Test",  
  "Type": "Test",
  "ComputerModel": "Testings"  
  "Properties": {
    "Access": "CUSTOMER"
   }
}

C# code:

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
    HttpRequest req, ILogger log)
{
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    string computermodel= req.Query["ComputerModel"]; // I don't  get anything here
    string responseMessage = string.IsNullOrEmpty(Computermodel) 
        ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
        : $"Hello, {name}. This HTTP triggered function executed successfully.";

    return new OkObjectResult(responseMessage);
}
Ankit Kumar
  • 476
  • 1
  • 11
  • 38
  • Post the *relevant* code. Have you tried something yet? There are a *lot* of JSON-related questions that show how to parse JSON data using the two most popular libraries. All ASP.NET MVC and Web API tutorials explain how to work with JSON or even have the framework parse the JSON payload directly into action parameters – Panagiotis Kanavos Jan 22 '21 at 08:39
  • 3
    BTW `{{...}}` is not JSON. A JSON string can contain either arrays or dictionaries, and dictionaries are specified using a single bracket. ALL JSON libraries will throw an error if they encounter `{{` or `}}`. With a valid JSON payload you could create an MVC or Web API action with `public ActionResult PostRun(Computer computer){...}` and the framework would deserialize the JSON string into a `Computer` instance – Panagiotis Kanavos Jan 22 '21 at 08:41
  • if you see the code , I am trying to extract the element in a way which is not working. yes i am googling my solution and meanwhile I have posted to get anything here.. whichever I get first :) – Ankit Kumar Jan 22 '21 at 08:43
  • What you posted has nothing to do with JSON or MVC/Web API actions. You can't google your way to programming. You have to understand the concepts at least, to avoid wasting time trying to implement what's already available – Panagiotis Kanavos Jan 22 '21 at 08:44
  • 3
    In fact, if you simply tried the `Getting Started` tutorials in the docs, you'd avoid the current problem. But `{{..}}` isn't JSON , no matter what – Panagiotis Kanavos Jan 22 '21 at 08:45
  • well, I may be wrong in formatting. I will check that and correct that, but from question it is very clear that I am trying to extract a particular element in JSON. Its not that I don't understand anything at all. – Ankit Kumar Jan 22 '21 at 08:48
  • 1
    `HttpTrigger` is used in Azure Functions. Don't expect people to guess what you're doing. The docs explain [Triggers and binding](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-example) and the example shows that the payload is automatically parsed into a JObject using JSON.NET – Panagiotis Kanavos Jan 22 '21 at 08:53
  • 1
    @AnkitKumar as mentioned by @PanagiotisKanavos, your JSON is not correct. It should be something like `{ "id": "Test", "Type": "Test", "ComputerModel": "Testings", "Properties": {"Access": "CUSTOMER" }}`. You can newtsonsoft package to convert to specific type or parse to JObject and read the value from `JObject` If you notice it has single `{` and has `,` after each property. – user1672994 Jan 22 '21 at 08:55
  • 1
    Once you have right Json as mentioned above you can do like this `var jObject = JObject.Parse(requestBody);` `var computerModel = jObject["ComputerModel"].ToObject();` – user1672994 Jan 22 '21 at 08:59
  • @AnkitKumar did you read the documentation? The payload can be parsed automatically into a JObject parameter. Without that, your question is identical to hundreds of similar SO questions. Instead of using `JObject.Parse` you can add a JObject parameter to your function – Panagiotis Kanavos Jan 22 '21 at 09:13
  • @user1672994 , you had the patience to read my question and provide a solution and it worked!!! please post it as an answer. – Ankit Kumar Jan 22 '21 at 09:16
  • Does this answer your question? [Receiving JSON data back from HTTP request](https://stackoverflow.com/questions/10928528/receiving-json-data-back-from-http-request) – Liam Jan 22 '21 at 09:21
  • @AnkitKumar - thanks I've posted the solution as answer. – user1672994 Jan 22 '21 at 09:27

1 Answers1

1

Your json seems not to be correct as discussed in comments. The correct Json will be as follows:

{
     "id": "Test", 
     "Type": "Test", 
     "ComputerModel": "Testings",   
     "Properties": {
                        "Access": "CUSTOMER" 
                    }
}

And using above Json you can parse it using JObject. req.Query gives the query string parameters but your json is posted as post body.

 var jObject = JObject.Parse(requestBody );
 Console.WriteLine(jObject["ComputerModel"].ToObject<string>());
user1672994
  • 10,509
  • 1
  • 19
  • 32