Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Parse Json in a Azure Function

"I have create a Azure Function in this function I call an API that returns JSON. I want to parse this JSON to an object so I can use it in the function. I cannot not use Newton.JSON as the function seems not to know this. How can I parse the JSON?"

like image 417
Boris Pluimvee Avatar asked Jun 29 '16 08:06

Boris Pluimvee


People also ask

Where is function json in Azure function?

Folder structure. The code for all the functions in a specific function app is located in a root project folder that contains a host configuration file. The host. json file contains runtime-specific configurations and is in the root folder of the function app.

What is json parse () method?

parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

What is json format in Azure?

Azure SQL Database and Azure SQL Managed Instance let you parse and query data represented in JavaScript Object Notation (JSON) format, and export your relational data as JSON text. The following JSON scenarios are available: Formatting relational data in JSON format using FOR JSON clause.

How do I parse json in Powerautomate?

Using 'parse JSON' action The value of content will be the 'body' value from 'Send an HTTP request to SharePoint. Now configure the schema correctly. In most cases this value is configured incorrectly which would result in null values. Click on 'Generate from sample' and then you will be getting the below pop-up.


1 Answers

Here is a complete Azure Function source code for serializing/deserializing objects using JsonNet:

#r "Newtonsoft.Json"

using System.Net;
using Newtonsoft.Json;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    dynamic body = await req.Content.ReadAsStringAsync();
    var e = JsonConvert.DeserializeObject<EventData>(body as string);
    return req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(e));
}

public class EventData
{
    public string Category { get; set; }
    public string Action { get; set; }
    public string Label { get; set; }
}

Sample input (request body):

{
    "Category": "Azure Functions",
    "Action": "Run",
    "Label": "Test"
}

Sample output:

"{\"Category\":\"Azure Functions\",\"Action\":\"Run\",\"Label\":\"Test\"}"
like image 58
Thomas C. G. de Vilhena Avatar answered Sep 20 '22 16:09

Thomas C. G. de Vilhena