Click here to Skip to main content
15,036,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);


What I have tried:

<pre> 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
Posted
Updated 17-Oct-19 22:03pm

1 solution

   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900