|
| 1 | +import XLSX from 'xlsx'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; // Make sure to import this |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | + |
| 9 | +// Paths to the Excel and JSON files |
| 10 | +const excelFilePath = path.join(__dirname, '../excel', 'hackathons.xlsx'); |
| 11 | +const jsonFilePath = path.join(__dirname, '../data', 'hackathons.json'); |
| 12 | + |
| 13 | +// Function to convert Excel to JSON with proper schema |
| 14 | +function convertExcelToJson() { |
| 15 | + // Read the Excel file |
| 16 | + const workbook = XLSX.readFile(excelFilePath); |
| 17 | + |
| 18 | + // Assuming data is in the first sheet |
| 19 | + const sheetName = workbook.SheetNames[0]; |
| 20 | + const sheet = workbook.Sheets[sheetName]; |
| 21 | + |
| 22 | + // Convert sheet data to JSON (object per row) |
| 23 | + const jsonData = XLSX.utils.sheet_to_json(sheet); |
| 24 | + |
| 25 | + // Transform rows into schema format |
| 26 | + const hackathons = jsonData.map((row) => ({ |
| 27 | + id: row["ID"] || "", |
| 28 | + title: row["Title"] || "", |
| 29 | + type: row["Type"] || "Scholarship", |
| 30 | + department: row["Department"] ? row["Department"].split(",").map((d) => d.trim()) : [], |
| 31 | + courseLevel: row["Course Level"] ? row["Course Level"].split(",").map((c) => c.trim()) : [], |
| 32 | + exclusive: row["Exclusive"] || "All", |
| 33 | + year: row["Year"] ? row["Year"].split(",").map((y) => y.trim()) : [], |
| 34 | + lastDateToApply: row["Last Date to Apply"] || "", |
| 35 | + stipend: row["Stipend"] || "", |
| 36 | + eligibility: row["Eligibility"] || "", |
| 37 | + description: row["Description"] || "", |
| 38 | + requirements: row["Requirements"] ? row["Requirements"].split(",").map((r) => r.trim()) : [], |
| 39 | + applicationLink: row["Application Link"] || "", |
| 40 | + category: row["Category"] || "", |
| 41 | + postedDate: row["Posted Date"] || "" |
| 42 | + })); |
| 43 | + |
| 44 | + // Wrap into object |
| 45 | + const finalJson = { hackathons }; |
| 46 | + |
| 47 | + // Save JSON file |
| 48 | + fs.writeFileSync(jsonFilePath, JSON.stringify(finalJson, null, 2), 'utf-8'); |
| 49 | + console.log('✅ Excel data has been converted to structured JSON!'); |
| 50 | +} |
| 51 | + |
| 52 | +// Run the conversion |
| 53 | +convertExcelToJson(); |
0 commit comments