Converting CSV to JSON in C#

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):

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

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.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store