Recipe 06: Type coercion and custom parsing
Current version: 3.1.0
Goal
Normalize data types and apply custom parsing logic.
Example
js
const { csvToJson, isDate } = require('jtcsv');
const csv = 'id,amount,active,created_at
1,9.99,true,2024-01-01';
const rows = csvToJson(csv, {
delimiter: ',',
parseNumbers: true,
parseBooleans: true,
transform: (row) => ({
...row,
created_at: isDate(row.created_at) ? new Date(row.created_at) : null
})
});
console.log(rows);Notes
parseNumbersandparseBooleanshandle the common cases.- Use
transformto normalize dates or custom enums.
Navigation
- Previous: Performance for large files
- Next: Special characters and encoding