I am playing with Azure Functions. However, I feel like I'm stumped on something pretty simple. I'm trying to figure out how to return some basic JSON. I'm not sure how to create some JSON and get it back to my request.
Once upon a time, I would create an object, populate its properties, and serialize it. So, I started down this path:
#r "Newtonsoft.Json" using System.Net; public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) { log.Info($"Running Function"); try { log.Info($"Function ran"); var myJSON = GetJson(); // I want myJSON to look like: // { // firstName:'John', // lastName: 'Doe', // orders: [ // { id:1, description:'...' }, // ... // ] // } return ?; } catch (Exception ex) { // TODO: Return/log exception return null; } } public static ? GetJson() { var person = new Person(); person.FirstName = "John"; person.LastName = "Doe"; person.Orders = new List<Order>(); person.Orders.Add(new Order() { Id=1, Description="..." }); ? } public class Person { public string FirstName { get; set; } public string LastName { get; set; } public List<Order> Orders { get; set; } } public class Order { public int Id { get; set; } public string Description { get; set; } }
However, I'm totally stuck on the serialization and return process now.I guess I'm used to returning JSON in ASP.NET MVC where everything is an Action
Using the Azure Function return valueIn a C# class library, apply the output binding attribute to the method return value. In Java, apply the output binding annotation to the function method. In other languages, set the name property in function. json to $return .
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.
To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.
Use Visual Studio Code and Azure Functions to rapidly create a serverless API, implement a RESTful architecture and safely store secure information like connection strings.
Here's a full example of an Azure function returning a properly formatted JSON object instead of XML:
#r "Newtonsoft.Json" using System.Net; using Newtonsoft.Json; using System.Text; public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) { var myObj = new {name = "thomas", location = "Denver"}; var jsonToReturn = JsonConvert.SerializeObject(myObj); return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonToReturn, Encoding.UTF8, "application/json") }; }
Navigate to the endpoint in a browser and you will see:
{ "name": "thomas", "location": "Denver" }
The easiest way is perhaps to
public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "/jsontestapi")] HttpRequest req, ILogger log) { return new JsonResult(resultObject); }
Will set the content-type to application/json
and return the json in the response body.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With