Last updated
Excel to JSON Conversion
Converting Excel files (.xlsx, .xls) to JSON makes spreadsheet data accessible to web applications and APIs. Each row in the spreadsheet becomes a JSON object, with column headers as keys. This is a common data migration task when moving from spreadsheet-based workflows to database-driven applications.
Excel File Structure
Modern Excel files (.xlsx) are ZIP archives containing XML files.
The main data is in xl/worksheets/sheet1.xml, with shared strings
in xl/sharedStrings.xml. Libraries like SheetJS (xlsx) handle
this complexity and provide a simple API.
Converting with SheetJS
import * as XLSX from 'xlsx';
async function excelToJson(file) {
const buffer = await file.arrayBuffer();
const workbook = XLSX.read(buffer, { type: 'array' });
const result = {};
for (const sheetName of workbook.SheetNames) {
const sheet = workbook.Sheets[sheetName];
// Convert to array of objects (first row = headers)
result[sheetName] = XLSX.utils.sheet_to_json(sheet, {
header: 1, // use first row as headers
defval: null, // default value for empty cells
blankrows: false // skip blank rows
});
}
return result;
}
// Usage with file input
document.getElementById('fileInput').addEventListener('change', async (e) => {
const file = e.target.files[0];
const json = await excelToJson(file);
console.log(JSON.stringify(json, null, 2));
});
Data Type Handling
- Dates: Excel stores dates as serial numbers (days since Jan 1, 1900). SheetJS converts them to JavaScript Date objects with
cellDates: true. - Formulas: By default, SheetJS reads the cached formula result. Use
raw: falseto get formatted values. - Numbers: Large numbers may lose precision — use
dense: truefor better handling.