Read CSV file from Request Body and turn it into JSON

How to read the CSV file sent in request body and turn it into JSON so you can use the data

Read CSV file from Request Body and turn it into JSON
Canonical URL
Do not index
Do not index
Related to Authors (1) (Content)
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
 
 

Join other 5k+ builders now!

Twice a month I share my learnings and insight on building products.

Subscribe
Pranav Malvawala

Full-Stack developer. Previously: Tweet Hunter, Taplio, Twemex