Today we will see how to convert CSV to JSON using ASP.Net C#. JSON is easiest format to read and write.
Understand CSV format:
Full form of CSV is Comma Separated Values, which means file stores tabular data in plain text format. A CSV document comprises of any number of records, Which can be separated by line breaks or something like that. Records are separated by some other character or string, most usually a comma or tab. Typically, all records have an identical grouping of fields.
Understand JSON format:
JSON stands for javascript object notation, which is open standard for transmitting data between server and web application. JSON is human readable format with attribute-value pair, and it is alternative of XML.
C# : Below is the asp.net c# method to convert CSV to JSON.
public void ConvertToJson()
{
try
{
if (FileUploadControl1.PostedFile.FileName != string.Empty)
{
string[] FileExt = FileUploadControl1.FileName.Split('.');
string FileEx = FileExt[FileExt.Length - 1];
if (FileEx.ToLower() == "csv")
{
string SourcePath = Server.MapPath("Resources//" + FileUploadControl1.FileName);
FileUploadControl1.SaveAs(SourcePath);
string DestinationPath = (Server.MapPath("Resources//" + FileExt[0] + ".json"));
StreamWriter sw = new StreamWriter(DestinationPath);
var csv = new List<string[]>();
var lines = System.IO.File.ReadAllLines(SourcePath);
foreach (string line in lines)
csv.Add(line.Split(','));
string json = new
System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);
sw.Write(json);
sw.Close();
MessageBox.Show("File is converted to JSON format.");
}
else
{
MessageBox.Show("Invalid File");
}
}
else
{
MessageBox.Show("CSV File Not Found.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Understanding of Code:{
try
{
if (FileUploadControl1.PostedFile.FileName != string.Empty)
{
string[] FileExt = FileUploadControl1.FileName.Split('.');
string FileEx = FileExt[FileExt.Length - 1];
if (FileEx.ToLower() == "csv")
{
string SourcePath = Server.MapPath("Resources//" + FileUploadControl1.FileName);
FileUploadControl1.SaveAs(SourcePath);
string DestinationPath = (Server.MapPath("Resources//" + FileExt[0] + ".json"));
StreamWriter sw = new StreamWriter(DestinationPath);
var csv = new List<string[]>();
var lines = System.IO.File.ReadAllLines(SourcePath);
foreach (string line in lines)
csv.Add(line.Split(','));
string json = new
System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);
sw.Write(json);
sw.Close();
MessageBox.Show("File is converted to JSON format.");
}
else
{
MessageBox.Show("Invalid File");
}
}
else
{
MessageBox.Show("CSV File Not Found.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
ConvertToJson method is written to convert CSV to JSON format. First of all we have imported CSV file and stored it in string array "String[] FileExt" by splitting it with '.' character. Next we have read file using System.IO.File.ReadAllLines() method, which is then split by ',' character and stored to list of string array "var csv = new List<string[]>();". Then it was serialised using javascript serializer "System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);". Here, we have saved JSON to external file. Which you can use based on your functional requirement.
Also Read:
0 Comments