Click here to Skip to main content
15,040,743 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to convert Datatable to JSON string using serialization.

Datatable has 2 lakhs records .i am using for each to take each row and convert to JSON.
But Application is not responding properly.

Please help

What I have tried:

I have tried with upto 10K records it seems to be working
Posted
Updated 30-Sep-20 22:56pm
Comments
Richard MacCutchan 1-Oct-20 5:06am
   
200,000 records in a single datatable? How long do you think it will take to read and process that JSON, if you ever get it converted? You need to rethink your design.
BillWoodruff 1-Oct-20 22:47pm
   
https://www.codeproject.com/Article.aspx?tag=2.MTU5NDUwOjA6NjM3MzcwMjc2ODI5NzI0MjI0&_z=1977390

We can't tell: we have no idea what your code is doing as we have no access to your HDD!

Have you tried NuGet Gallery | Newtonsoft.Json 12.0.3[^] - it normally works pretty well. You can serialize a dataset / datatable in a single line of code: Serialize a DataSet[^]
   
Why are you trying to serialize individual rows? You are performing a couple of thousand operations here when serializing the entire data set will only do one serialization operation. Change the code to serialize everything and you should see improvements.
   
At a guess, you're serializing each row to a string and concatenating it to a larger string - eg:
C#
// DON'T DO THIS!!!
string result = "";
foreach (var row in table)
{
    result += SerializeToJson(row);
}
String concatenation in a loop is horrendously slow. For each row, you have to create a new string, copy the characters from the existing string, append the characters from the new string, and store the result in the variable.

Instead, you should use a StringBuilder[^], or stream the results to the output directly.
C#
var sb = new StringBuilder();
foreach (var row in table)
{
    sb.Append(SerializeToJson(row));
}

string result = sb.ToString();

// Or:
using (Stream stream = File.Create("... some path ..."))
using (TextWriter writer = new StreamWriter(stream))
{
    foreach (var row in table)
    {
        writer.Write(SerializeToJson(row));
    }
}

// Or, using JSON.NET:
using (Stream stream = File.Create("... some path ..."))
using (TextWriter writer = new StreamWriter(stream))
using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
{
    JsonSerializer serializer = new JsonSerializer();
    foreach (var row in table)
    {
        serializer.Serialize(jsonWriter, row);
    }
}
How to concatenate multiple strings (C# Guide) | Microsoft Docs[^]
   

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