dev1/src/utils/convert-to-json.js

27 lines
834 B
JavaScript

import xlsx from 'xlsx';
import { writeFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
// Get the directory name
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Define file paths
const excelFilePath = join(__dirname, '../../Institution_data.xlsx');
const jsonFilePath = join(__dirname, 'Institution_data.json');
// Load Excel file
const workbook = xlsx.readFile(excelFilePath);
// Select the first worksheet
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
// Convert worksheet to JSON
const jsonData = xlsx.utils.sheet_to_json(worksheet);
// Write JSON data to file
writeFileSync(jsonFilePath, JSON.stringify(jsonData, null, 2));
console.log('JSON file created successfully at', jsonFilePath);