DEV Community

Cover image for How to Handle CSV, Excel, and JSON Uploads in Node.js (Without Losing Your Mind)
Abrar ahmed
Abrar ahmed

Posted on

1 1 1 1 1

How to Handle CSV, Excel, and JSON Uploads in Node.js (Without Losing Your Mind)

Ever found yourself trying to build a feature for users to upload files, and suddenly you're knee-deep in weird CSV quirks, Excel formats, and complex JSON structures?
I’ve been there too. Here’s a simple guide to help you handle file uploads in Node.js without losing your mind.

Step 1: Accept File Uploads (With Multer)

First, use multer to accept file uploads in Express:

npm install multer
Enter fullscreen mode Exit fullscreen mode

Basic setup:

const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();

app.post('/upload', upload.single('file'), (req, res) => {
  console.log(req.file);
  res.send('File uploaded!');
});
Enter fullscreen mode Exit fullscreen mode

Step 2: Parse Different File Types

1. CSV Files

Use csv-parser:

npm install csv-parser
Enter fullscreen mode Exit fullscreen mode

Code:

const fs = require('fs');
const csv = require('csv-parser');

fs.createReadStream('uploads/file.csv')
  .pipe(csv())
  .on('data', (row) => {
    console.log(row);
  });
Enter fullscreen mode Exit fullscreen mode

2.Excel Files

Excel Files

npm install xlsx
Enter fullscreen mode Exit fullscreen mode

Code:

const xlsx = require('xlsx');

const workbook = xlsx.readFile('uploads/file.xlsx');
const sheetName = workbook.SheetNames[0];
const data = xlsx.utils.sheet_to_json(workbook.Sheets[sheetName]);

console.log(data);
Enter fullscreen mode Exit fullscreen mode

3.JSON Files

Simple:

const fs = require('fs');

const data = JSON.parse(fs.readFileSync('uploads/file.json', 'utf8'));
console.log(data);
Enter fullscreen mode Exit fullscreen mode

Step 3: Handle Common Data Issues

  • Normalize date formats (e.g., with dayjs)
  • Remove empty rows
  • Deduplicate entries
  • Validate column headers

Example (normalize phone numbers):

function cleanPhoneNumber(num) {
  return num.replace(/\D/g, '');
}

Enter fullscreen mode Exit fullscreen mode

Step 4: Structure Your Code

  • Create a separate module for each file type
  • Keep upload logic separate from parsing
  • Log errors clearly

Final Thoughts

Handling messy files is something you’ll encounter while creating real-world apps. But don’t worry! With the right tools, you can easily work with CSV, Excel, and JSON files without losing your mind.

Got your own tips or tools? Drop them in the comments!

Dynatrace image

Frictionless debugging for developers

Debugging in production doesn't have to be a nightmare.

Dynatrace reimagines the developer experience with runtime debugging, native OpenTelemetry support, and IDE integration allowing developers to stay in the flow and focus on building instead of fixing.

Learn more

Top comments (0)

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay