Ad

Why I Am Getting Array JSON Response When I Call Method From Web API?

I am getting array which include my JSON when I call that method in postman like

 [
    {
        "spark_version": "7.6.x-scala2.12"
    }
]

API Method

[HttpGet]
    public IActionResult GetTest(int ActivityId)
    {
        string StoredJson = "exec sp_GetJobJSONTest " +
            "@ActivityId = " + ActivityId ;
        var result =  _context.Test.FromSqlRaw(StoredJson);
        return Ok(result);
    }

I want to get rid from square brackets [ ] in my response. How should I do that ?

Ad

Answer

for getting first row need to use .FirstOrDefault()

[HttpGet]
    public IActionResult GetTest(int ActivityId)
    {
        string StoredJson = "exec sp_GetJobJSONTest " +
            "@ActivityId = " + ActivityId ;
        var result =  _context.Test.FromSqlRaw(StoredJson).ToList().FirstOrDefault();
        return Ok(new {details = result };);
    }
 
Ad
source: stackoverflow.com
Ad