Converting Between JSON and CSV In C#

SuperConvert is an open-source lightweight package to convert between JSON, CSV, and Datatables,

It also contains a Hijri date helper (which will be discussed in future articles)

SuperConvert Download

Let's create our project and name it (SuperConvert now is supporting .netcore3.1, .net5, and .net6)

Converting between json and CSV in C#

First for converting JSON strings into CSV files we just have to install the SuperConvert package as follows:

  • Run the following NuGet command (recommended to install the latest version always)
    Install-Package SuperConvert -Version 1.0.3.2
  • We can install it from the NuGet package gallery by typing SuperConvert in the search bar and clicking install to the specific project
    Converting between json and CSV in C#

Please Install the latest version always.

After installing SuperConvert let's do some coding.

First, we have to type the using statement in order to be able to use the extension methods 

//1 Convert json to csv
using SuperConvert.Extentions;

Second, declare the JSON string, we can get it from a file, HTTP, Database .. etc

After getting your JSON List of objects you can use the extension method .ToCsv(), we can pass the path and the file name to the method or we can just call it without parameters (in this case the file will be saved to your application directory)

string jsonStr = "[{\"Id\":\"1\",\"Name\":\"Esam\",\"Gender\":\"Male\",\"Age\":25}," +
    "{ \"Id\": \"2\", \"Name\": \"Mohammed\", \"Gender\": \"Male\", \"Age\": 25 }]";
string csvPath = jsonStr.ToCsv();
Console.WriteLine(csvPath);

Another code

Converting between json and CSV in C#

The execution will be like 

Converting between json and CSV in C#

And the excel file saved 

Converting between json and CSV in C#


Recommended Free Ebook
Similar Articles