Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a csv file to json using C#

Tags:

json

c#

csv

I was wondering if someone's written a utility to convert a CSV file to Json using C#. From a previous question on stackoverflow, I'm aware of this nice utility - https://github.com/cparker15/csv-to-json and at the moment I plan to refer to it but an existing C# implementation would be very helpful! Thanks!

like image 828
user1427026 Avatar asked May 30 '12 20:05

user1427026


People also ask

Can we convert string to JSON in C?

Convert String to JSON Object With the JObject. Parse() Function in C# The JObject class inside the Newtonsoft. Json package is used to represent a JSON object in C#.


2 Answers

If you can use System.Web.Extensions, something like this could work:

var csv = new List<string[]>(); // or, List<YourClass> var lines = System.IO.File.ReadAllLines(@"C:\file.txt"); foreach (string line in lines)     csv.Add(line.Split(',')); // or, populate YourClass           string json = new      System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv); 

You might have more complex parsing requirements for the csv file and you might have a class that encapsulates the data from one line, but the point is that you can serialize to JSON with one line of code once you have a Collection of lines.

like image 93
mafue Avatar answered Sep 29 '22 22:09

mafue


Cinchoo ETL - an open source library available to do the conversion of CSV to JSON easily with few lines of code

For a sample CSV:

Id, Name, City 1, Tom, NY 2, Mark, NJ 3, Lou, FL 4, Smith, PA 5, Raj, DC 

Sample code,

string csv = @"Id, Name, City 1, Tom, NY 2, Mark, NJ 3, Lou, FL 4, Smith, PA 5, Raj, DC ";  StringBuilder sb = new StringBuilder(); using (var p = ChoCSVReader.LoadText(csv)     .WithFirstLineHeader()     ) {     using (var w = new ChoJSONWriter(sb))         w.Write(p); }  Console.WriteLine(sb.ToString()); 

Output JSON:

[  {   "Id": "1",   "Name": "Tom",   "City": "NY"  },  {   "Id": "2",   "Name": "Mark",   "City": "NJ"  },  {   "Id": "3",   "Name": "Lou",   "City": "FL"  },  {   "Id": "4",   "Name": "Smith",   "City": "PA"  },  {   "Id": "5",   "Name": "Raj",   "City": "DC"  } ] 

Sample fiddle: https://dotnetfiddle.net/pclnsT

Checkout CodeProject article for some additional help.

UPDATE: If your CSV file has duplicate column names or no names, please use the below steps to produce the JSON file

string csv = @"Id, Name,  1, Tom, NY 2, Mark, NJ 3, Lou, FL 4, Smith, PA 5, Raj, DC ";  StringBuilder sb = new StringBuilder(); using (var p = ChoCSVReader.LoadText(csv)     .WithField("Id", position: 1)     .WithField("Name", position: 2)     .WithField("City", position: 3)     .WithFirstLineHeader(true)     ) {     using (var w = new ChoJSONWriter(sb))         w.Write(p); }  Console.WriteLine(sb.ToString()); 

Sample fiddle: https://dotnetfiddle.net/pP5Du6

Disclaimer: I'm the author of this library.

like image 22
Cinchoo Avatar answered Sep 29 '22 23:09

Cinchoo