How to Convert CSV to JSON in Nodejs

Cloudmersive
2 min readMay 9, 2024

--

Using just a few lines of Nodejs code, we can easily convert CSV files to JSON objects.

We can copy from the complementary, ready-to-run code examples provided below to call a free API that handles the CSV to JSON conversion for us. We’ll get consistent, reliable conversions this way without having to write any new code ourselves.

First, let’s install the SDK using NPM install:

npm install cloudmersive-convert-api-client --save

Alternatively, we can add the Node client to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Now we can copy the remaining code examples to call the function. We’ll need a free Cloudmersive API key to authorize our API calls (this will give us a limit of 800 API calls per month with no additional commitments:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.ConvertDataApi();

var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.

var opts = {
'columnNamesFromFirstRow': true // Boolean | 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.
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDataCsvToJson(inputFile, opts, callback);

All done — no more code required! Now we can easily take care of our CSV to JSON conversions with just a few lines of Nodejs code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet