To read CSV from a file and convert it into a JSON object in Typescript, we need to use the 'csv-parser' and 'fs' modules from node.js.
Here's the sample code:
index.ts617 chars21 lines
The above code reads the CSV file from the given file path using 'fs' module and pipes it to the 'csv-parser' module. The 'csv-parser' module parses the CSV file and emits a 'data' event for each row in the CSV file. We push each row to an array and finally resolve the promise with the array of JSON objects.
We can use this function to read any CSV file, and convert it into a JSON object.
gistlibby LogSnag