In previous article, I mentioned Convert JSON to CSV in C# and but now in this article, I have provided working example to convert CSV to JSON using C# console application (.NET Core)

Step 1: You can create a blank new console application in Visual Studio, by opening it and selecting "Create new project" -> "Console app" -> Select .NET version and click ok.

Step 2: If you have created new application or using already created one, you need to install "Newtonsoft.Json" NuGet Package, so navigate to "Tools" -> "Nuget Package Manager" -> "Package manager console", then use below command

Install-Package Newtonsoft.Json

and Visual Studio will install Newtonsoft.Json automatically.

Step 3: In your program.cs, you can use below C# code to convert CSV to JSON

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace CSVtoJSON
    internal class Program
        static void Main(string[] args)
            var csv = new List<string[]>(); 
            var lines = System.IO.File.ReadAllLines(@"D:\Orders.csv"); // csv file location

            // loop through all lines and add it in list as string
            foreach (string line in lines)

            //split string to get first line, header line as JSON properties
            var properties = lines[0].Split(',');

            var listObjResult = new List<Dictionary<string, string>>();

            //loop all remaining lines, except header so starting it from 1
            // instead of 0
            for (int i = 1; i < lines.Length; i++)
                var objResult = new Dictionary<string, string>();
                for (int j = 0; j < properties.Length; j++)
                    objResult.Add(properties[j], csv[i][j]);


            // convert dictionary into JSON
            var json = JsonConvert.SerializeObject(listObjResult);


and here is the output when I check "json" variable value


and here is the original CSV file

CSV to JSON c#

So as you can see from above Visual Studio output, headers are converted into "properties" of JSON.

In C# code, we are first reading complete CSV file and then looping each line, using foreach

Once we have all lines(rows) of CSV, then we are taking only first line as header.

Then looping all remaining lines and saving it as dictionary key value (JSON property and value).

Once we have dictionary values, we are converting it into JSON using Newtonsoft.JSON.

You may also like to read:

Converting JSON to XML OR XML to JSON using C#

Get only time from Datetime in C#?

How to add time in datetime in C#?

Serialize List to JSON in C#

Convert CSV to JSON using Javascript