Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created November 29, 2022 11:45
How to Convert CSV to JSON in Node.js
var fs = require('fs');
var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// Instantiate an instance of the LoadOptions class and set the format to CSV.
var loadOptions = new aspose.cells.LoadOptions(aspose.cells.LoadFormat.CSV);
// Load CSV file by initializing the constructor by Workbook class.
var workbook = new aspose.cells.Workbook( "sample.csv", loadOptions);
// Invoke the getLastCell method to get the last cell in this worksheet. Returns null if there is no data in the worksheet.
var lastCell = workbook.getWorksheets().get(0).getCells().getLastCell();
// Set exporting range to json by calling the ExportRangeToJsonOptions method.
var options = new aspose.cells.ExportRangeToJsonOptions();
// Call the createRange method to create a Range object from a range of cells.
var range = workbook.getWorksheets().get(0).getCells().createRange(0, 0, lastCell.getRow() + 1, lastCell.getColumn() + 1);
// Export the range to json file by calling the exportRangeToJson method.
var data = aspose.cells.JsonUtility.exportRangeToJson(range, options);
// Display JSON
console.log(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment