Adhikar Patil

Adhikar Patil

  • 1.6k
  • 240
  • 9k

How to convert Json Object to DataTable in Asp.Net Core

Oct 17 2019 10:21 PM
Hello,
I want to convert Json object to DataTable. I have json file to insert bulk data into table using SqlBulkCopy
string contentRootPath = _appSettings.MasterJsonPath;
var folderDetails = Path.Combine(Directory.GetCurrentDirectory(),$"wwwroot\\{"MasterData\\masterdata.json"}");
var JSON = System.IO.File.ReadAllText(folderDetails);
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(JSON);
var response = 0;
DataTable dt = (DataTable)jsonObj["M203StructureKind"].AsDataReader();
SqlBulkCopy bulk = new SqlBulkCopy("Data Source = DESKTOP-JENGCSC;
Initial Catalog=BdsDb; Integrated Security=True;");
bulk.DestinationTableName = "M203StructureKind";
bulk.WriteToServer(dt);
 
So i am getting exception like Cannot convert type 'Newtonsoft.Json.Linq.JArray' to 'System.Data.DataTable while converting the json object to datatable 

Answers (1)