0

OK, so my situation is this: I have to call a remote webservice that returns data as a json object. I need to take this data and map it an existing table in a database. So, I need to map this data and then add it to the existing table in the most efficient manner possible. After doing to research the easiest way to convert the data to a usable type is to create an c# class that matches the json object, and then create a DataTable object that matches my database table, and the map the 2, and do a SQLBulkCopy to put the data into the table. But is that really the best way to do this? I end up having to map the from the JSON to a class and then map that class to the datatable, and then map the datatable to the database entity, if this the way to do this ok, but it seems to me there should be a much less convoluted way of doing this?

Sample Code:

JSON Data:

[
 { 
    "FirstName":"John",
    "LastName":"Doe", 
    "Address":"123 Main St."
 },
 {
     "FirstName":"Jane",
     "LastName":"Doe", 
     "Address":"123 Main St."
 }
]

Class Object:

public class ContactConversion
{
    public string FirstName {get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
}

apiReturnData = remotewebserviceCall();
List<ContactConversion> contacts = JsonConvert.DeserializeObject<List<ContactConversion>>(apiReturnData);
DataTable leads = CreateTempLeadsTable();
DoBulkCopy(DataTable, contacts);
Kelso Sharp
  • 972
  • 8
  • 12
  • Seem like very little code to get the job done and if runs in a reasonable amount of time then what is the problem exactly? – Paul C Apr 11 '14 at 15:51
  • Its not that is bad per sae, It just seems excessive, I have to bring in the Json object, create a matching class and map to the class, then create a datatable, map the class to the datatable, and then pass the datatable or datareader to SqlBulkCopy, and map again to the sql table. Having to map to that many different object seems excessive and I am wondering if I was missing something, a lot of actual code in this example is omitted for brevity. – Kelso Sharp Apr 15 '14 at 15:26

0 Answers0