Search code examples
c#jsondatatable

Get json response data from datatable


I have a code that works today, and wondering if i could do this code easier/faster? The response data is about 9500 rows every time

IS it possible to get the objects from the json data without having to go true every "row" with for each?

dynamic obj = JsonConvert.DeserializeObject<object>(response.Content);
dynamic alloweditemsdata = obj.allowedItemIds;
dynamic responseId = obj.responseId;
dt.PrimaryKey = new DataColumn[1] { dt.Columns["number"] };
string resp = obj.GetValue("responseId").ToString();
foreach (object item3 in alloweditemsdata)
{
    int alloweditem = (int)(dynamic)item3;
    DataRow Drw = dt.Rows.Find(alloweditem);
    object numberresult = Drw.ItemArray[0];
    object surnameresult = Drw.ItemArray[1];
    object forenameresult = Drw.ItemArray[2];
    object emailAddressresult = Drw.ItemArray[3];
    string taxidentifierresult = (string)Drw.ItemArray[4];
    string clientDetails = " " + numberresult?.ToString() + ",\"" + surnameresult?.ToString() + "\",\"" + forenameresult?.ToString() + "\",\"" + emailAddressresult?.ToString() + "\",\"" + responseId + "\",\"" + daytime + "\"" + Environment.NewLine;
    File.AppendAllText(newFileName, clientDetails);

Solution

  • Get rid of dynamic typing - it's very slow.

    Create a specific type and deserialize it.

    These two strings get the same value:

    dynamic responseId = obj.responseId;
    string resp = obj.GetValue("responseId").ToString();
    

    Moreover, the second is not used anywhere. Be careful not to repeat unnecessary work.

    Switch from Newtonsoft.Json library to a more performant System.Text.Json library.
    If possible, use source generation.

    Do not use string concatenation. Apply, for example, StringBuilder.

    But the biggest slug in your code is probably the File.AppendAllText call. This method opens and closes the file every time it is called! And these are very slow operations!
    Use StreamWriter like this:

    using var writer = new StreamWriter(newFileName);
    
    foreach (object item3 in alloweditemsdata)
    {
        ...
        writer.WriteLine(clientDetails);
    }
    

    Also, if possible, use asynchrony.