Hi @Nguyen Yann
There are two options to go about getting that result. The first is using the FOR JSON
clause in your T-SQL statement. I didn't see anything in the docs illustrating but it's something worth testing.
SELECT ProductId, JSON_QUERY(JSON_OBJECT('Name', Name, 'Cost', Cost)) AS clothes FROM Products WHERE Cost = @Cost FOR JSON PATH
The second option is serializing the output yourself.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Samples.InputBindingSamples
{
public static class GetProductsString
{
[FunctionName("GetProductsString")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "getproducts-string/{cost}")] HttpRequest req,
[Sql("select * from Products where cost = @Cost", "SqlConnectionString", parameters: "@Cost={cost}")]
IEnumerable<Product> products)
{
// Products is a JSON representation of the returned rows.
// Serialize the input in the desired format.
var serializedProducts = new List<object>();
foreach (var product in products)
{
var serializedProduct = new
{
ProductId = product.ProductId,
clothes = new
{
Name = product.Name,
Cost = product.Cost
}
};
serializedProducts.Add(serializedProduct);
}
var json = JsonConvert.SerializeObject(serializedProducts);
return new OkObjectResult(json);
}
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Cost { get; set; }
}
}