An Azure service that provides an event-driven serverless compute platform.
just return what you want:
var data = new { authenticated = false };
string json = JsonSerializer.Serialize(data);
response.WriteString(json);
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I have the below function which I expect to return the following when authenticated
{
"authenticated": true,
"member_id": 123,
"first_name": "my first name",
"last_name": "my last name"
}
but when authenticated is false then it should only return one row like this:
{
"authenticated": true,
}
This is my Azure Function:
namespace MyApp;
public class MyFunction
{
private readonly ILogger _logger;
public ActivationSeconds(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MyFunction>();
}
[Function("MyFunction")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "MyFunction")] HttpRequestData req,
string id,
string email)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "application/json; charset=utf-8");
var connectionString = "Server=server11.database.windows.net;Database=db;User Id=myuser;Password=MyPassword;";
var query = "EXEC get_data @id, @email";
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (var command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@id", id);
command.Parameters.AddWithValue("@email", email);
var reader = command.ExecuteReader();
var dataList = new List<ActivationSecondsData>();
var authenticated = false;
while (reader.Read())
{
authenticated = Convert.ToBoolean(reader["authenticated"]);
if (authenticated)
{
var data = new ActivationSecondsData
{
authenticated = true,
member_id = (Int64)reader["member_id"],
};
string json = JsonSerializer.Serialize(data);
response.WriteString(json);
}
else
{
var data = new ActivationSecondsData { authenticated = false };
string json = JsonSerializer.Serialize(data);
response.WriteString(json);
}
}
}
}
return response;
}
}
It is working when authenticated is true but when authenticated i sfalse I get the following:
{
"authenticated": false,
"member_id": 0,
"first_name": null,
"last_name": null
}
How can I fix this?
Thanks,
Jassim
An Azure service that provides an event-driven serverless compute platform.
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
just return what you want:
var data = new { authenticated = false };
string json = JsonSerializer.Serialize(data);
response.WriteString(json);
Hi @Anonymous ,
You can fix the issue in couple of different ways:
member_id, first_name, and last_name nullable (int? and string), their default values will be null when authenticated is false and use decorator [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)].public class ActivationSecondsData
{
public bool authenticated { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? member_id { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string first_name { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string last_name { get; set; }
}
var data = new { authenticated = false };
string json = JsonSerializer.Serialize(data);
Hope this helps.
Kind Regards,
Nisha