I work with JSON / CSV files in my asp.net web API project and tried with CSVHelper and ServiceStack.Text , but could not get it working.
The JSON file containing the array is dynamic and can have any number of fields
I read the file using streamreader and then have to convert it to a CSV file to make it downloadable to end users.
sample file text
[{"COLUMN1":"a","COLUMN2":"b","COLUMN3":"c","COLUMN4":"d","COLUMN5":"e"}, {"COLUMN1":"a","COLUMN2":"b","COLUMN3":"c","COLUMN4":"d","COLUMN5":"e"}]
JSON for CSV
public static string jsonStringToCSV(string content) { var jsonContent = (JArray)JsonConvert.DeserializeObject(content); var csv = ServiceStack.Text.CsvSerializer.SerializeToCsv(jsonContent); return csv; }
It does not give me CSV data

Then some files are a comma or tab delimiter type, and I want to use CSVHelper to convert a CSV string to dynamically IEnumerable
public static IEnumerable StringToList(string data, string delimiter, bool HasHeader) { using (var csv = new CsvReader(new StringReader(data))) { csv.Configuration.SkipEmptyRecords = true; csv.Configuration.HasHeaderRecord = HasHeader; csv.Configuration.Delimiter = delimiter; var records = csv.GetRecords(); return records; } }
source share