Converting CSV to JSON in C#

Cloudmersive
2 min readFeb 19, 2021

If you have a CSV file with data that needs to be transmitted to a web application, you may find the process tough to navigate. But, if you convert the file to a JSON format, you will be able to preserve the integrity of the data, while improving the online exchange. The following conversion API can assist in simplifying your CSV to JSON file transformation.

First off, we will install the .NET Framework SDK (.NET Core also available):

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

Then we can configure the API key, instance the API, and call the function with the following code:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertDataCsvToJsonExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new ConvertDataApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
var columnNamesFromFirstRow = true; // bool? | Optional; If true, the first row will be used as the labels for the columns; if false, columns will be named Column0, Column1, etc. Default is true. Set to false if you are not using column headings, or have an irregular column structure. (optional)
try
{
// Convert CSV to JSON conversion
Object result = apiInstance.ConvertDataCsvToJson(inputFile, columnNamesFromFirstRow);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDataApi.ConvertDataCsvToJson: " + e.Message );
}
}
}
}

And just like that, we’re done! No hassle required.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.