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 cr
It looks like this can be achieved just by using the "application/json" media type, without the need to explicitly serialize Person
with Newtonsoft.Json
.
Here is the full working sample that results in Chrome as:
{"FirstName":"John","LastName":"Doe","Orders":[{"Id":1,"Description":"..."}]}
The code is given as below:
[FunctionName("StackOverflowReturnJson")]
public static HttpResponseMessage Run([HttpTrigger("get", "post", Route = "StackOverflowReturnJson")]HttpRequestMessage req, TraceWriter log)
{
log.Info($"Running Function");
try
{
log.Info($"Function ran");
var myJSON = GetJson(); // Note: this actually returns an instance of 'Person'
// I want myJSON to look like:
// {
// firstName:'John',
// lastName: 'Doe',
// orders: [
// { id:1, description:'...' },
// ...
// ]
// }
var response = req.CreateResponse(HttpStatusCode.OK, myJSON, JsonMediaTypeFormatter.DefaultMediaType); // DefaultMediaType = "application/json" does the 'trick'
return response;
}
catch (Exception ex)
{
// TODO: Return/log exception
return null;
}
}
public static Person GetJson()
{
var person = new Person();
person.FirstName = "John";
person.LastName = "Doe";
person.Orders = new List<Order>();
person.Orders.Add(new Order() { Id = 1, Description = "..." });
return person;
}
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; }
}
JSON is pretty easy, Newtonsoft.Json library is a special case. You can include it by adding this at the top of the script file:
#r "Newtonsoft.Json"
using Newtonsoft.Json;
Then your function becomes:
public static string GetJson()
{
var person = new Person();
person.FirstName = "John";
person.LastName = "Doe";
person.Orders = new List<Order>();
person.Orders.Add(new Order() { Id=1, Description="..." });
return JsonConvert.SerializeObject(person);
}
You can change the method signature into:
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
and it will allow JSON data to be returned.
You can take req
from
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
and create the response using
return req.CreateResponse(HttpStatusCode.OK, json, "application/json");
or any of the other overloads in assembly System.Web.Http
.
More info on docs.microsoft.com
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.
I had a similar issue and this seemed to be the most popular post with no answer. After figuring what node does the below should work and give you exactly what you are after. The other examples still returns a string representation wheres this will return JSON.
Remember to declare using System.Text; and also add:
return JsonConvert.SerializeObject(person);
to the GetJson function as per Juunas response.
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(GetJson(), Encoding.UTF8, "application/json")
};