Assuming you have a DataTable dt
then you can project the columns into a list of anonymous type (containing ColumnName and Type) using Linq like this:
var columns = dt.Columns
.Cast<DataColumn>()
.Select(c => new { Name = c.ColumnName, Type = c.DataType.ToString()});
Then using Newtonsoft.Json
you can serialize like this:
var json = JsonConvert.Serialize(columns);
Or using System.Text.Json
:
var json = JsonSerializer.Serialize(columns);
Which (if your table has 3 columns - Id
, Name
, Date
) will produce JSON something like this:
[
{
"Name": "Id",
"Type": "System.Int32"
},
{
"Name": "Name",
"Type": "System.String"
},
{
"Name": "Date",
"Type": "System.DateTime"
}
]