Skip to content

XLSX -> JSON [Node.js Web App]

This is a simple node application that takes in an xlsx (pulled from rock), converts it to JSON (do whatever you want w that) and then also sends it via webhook to Rock RMS.

Gihub Repo: XLSX -> JSON

I simply ran this application locally on my machine. No reason to deploy publically tbh.

Here is the api endpoint @ index.js:

javascript
app.post('/upload', upload.single('file'), async (req, res) => {
    try {
      if (!req.file) {
        return res.status(400).json({ error: 'No file uploaded' });
      }
  
      const filePath = req.file.path;
  
      if (!fs.existsSync(filePath)) {
        return res.status(400).json({ error: 'File not found after upload' });
      }
  
      const workbook = xlsx.readFile(filePath);
      const sheet_name_list = workbook.SheetNames;
  
      const jsonData = xlsx.utils.sheet_to_json(workbook.Sheets[sheet_name_list[0]], { header: 1 });
  
      if (jsonData.length < 3) {
        return res.status(400).json({ error: 'Invalid file format' });
      }
  
      const headers = jsonData[2];
      const formattedData = jsonData.slice(3).map(row =>
        Object.fromEntries(headers.map((header, i) => [header || `Column_${i}`, row[i] || null]))
      );
  
      res.status(200).json({ message: 'File successfully processed as json.', data: formattedData });
  
      axios.post( WEBHOOK_URL, formattedData)
        .then(() => console.log('✅ Data successfully sent to webhook'))
        .catch(err => console.error('❌ Error sending data to webhook:', err));
  
    } catch (error) {
      console.error('❌ Error processing file:', error);
      res.status(500).json({ error: 'Error processing file' });
    }
  });

Explore and learn. Released under the MIT License.