Let’s say you’ve sent a CSV file in formdata. The key for the CSV file is
myCsvFile
.
To read the data as JSON, you’ll have to install csvtojson
package and do the following,import csvtojson from "csvtojson"
// your usual code...
const formdata = await request.formdata() // request is simple request object.
const csvFile = formdata.get('myCsvFile')
const json = await csvtojson().fromString(await csvFile.text())
Yes, it is that simple!
If you’re using cloudflare workers, I found this article very helpful → https://blog.remeika.us/2023/07/07/parsing-csv-files.html