I need to create a json from a regular column to a json with a key. Only I don't understand how to do it in c#. I am using Postgresql database and I need to keep separate data there, and I need to pass to the server via api as I indicated below
What I got is.
{
"id": '',
"title": "",
"description": "",
"image": "",
"question": ""
}
to
{
"id": {
"title": "",
"description": "",
"image": "",
"question": ""
}
}
Here's my code
[HttpGet]
public JsonResult Get(int id)
{
string query =
@"
select id as ""id"",
title as ""title"",
description as ""description"",
image as ""image"",
question as ""question""
from card";
DataTable dt = new DataTable();
string sqlDataSource =
_configuration.GetConnectionString("EmployeeAppCon");
NpgsqlDataReader myReader;
using (NpgsqlConnection myCon = new NpgsqlConnection(sqlDataSource))
{
myCon.Open();
using (NpgsqlCommand myCommand = new NpgsqlCommand(query, myCon)
)
{
myReader = myCommand.ExecuteReader();
dt.Load (myReader);
myReader.Close();
myCon.Close();
}
}
string temp = JsonConvert.SerializeObject(dt);
return new JsonResult(dt);
}
idcontain unique values?