The mapRows() feature allows you to transform, filter, or enrich each CSV row after conversion to JSON.
const csvToJson = require('convert-csv-to-json');
// Transform each row - uppercase the firstName
const result = csvToJson
.fieldDelimiter(',')
.mapRows((row, index) => {
row.firstName = row.firstName.toUpperCase();
return row;
})
.getJsonFromCsv('input.csv');// Add computed properties to each row
const result = csvToJson
.fieldDelimiter(',')
.mapRows((row, index) => {
row.rowNumber = index + 1;
row.fullName = `${row.firstName} ${row.lastName}`;
return row;
})
.csvStringToJson(csvString);// Filter out rows that don't match a condition
const result = csvToJson
.fieldDelimiter(',')
.mapRows((row) => {
// Only keep rows where age >= 30
if (parseInt(row.age) >= 30) {
return row;
}
return null; // Filters out this row
})
.getJsonFromCsv('input.csv');// Transform the entire row structure
const result = csvToJson
.fieldDelimiter(',')
.mapRows((row, index) => {
return {
id: index,
person: {
name: `${row.firstName} ${row.lastName}`,
email: row.email
},
metadata: {
processed: true,
timestamp: new Date().toISOString()
}
};
})
.getJsonFromCsv('input.csv');Parameters:
mapperFunction(Function): A callback function that transforms each row- Parameters:
row(Object): The JSON object representing the current rowindex(Number): The 0-based index of the current row
- Return value:
- Return the (possibly modified) row object to keep the row
- Return
nullorundefinedto filter out the row
- Parameters:
Returns: this for method chaining
Throws: TypeError if mapperFn is not a function
const result = csvToJson
.fieldDelimiter(',')
.formatValueByType(true)
.mapRows((row) => {
// Clean email
row.email = row.email.toLowerCase().trim();
// Validate age
if (row.age < 0 || row.age > 150) {
return null; // Filter invalid rows
}
return row;
})
.getJsonFromCsv('data.csv');const result = csvToJson
.fieldDelimiter(',')
.mapRows((row, index) => {
// Add ID and processing date
row.id = index + 1000;
row.processedDate = new Date().toISOString();
// Categorize by age
if (row.age < 18) {
row.category = 'minor';
} else if (row.age < 65) {
row.category = 'adult';
} else {
row.category = 'senior';
}
return row;
})
.getJsonFromCsv('people.csv');const csvString = 'id,name,score\n1,Alice,95\n2,Bob,87\n3,Charlie,92';
const result = csvToJson
.fieldDelimiter(',')
.mapRows((row) => {
// Convert score to number and add grade
const score = parseFloat(row.score);
row.score = score;
row.grade = score >= 90 ? 'A' : score >= 80 ? 'B' : 'C';
return row;
})
.csvStringToJson(csvString);
// Result:
// [
// { id: '1', name: 'Alice', score: 95, grade: 'A' },
// { id: '2', name: 'Bob', score: 87, grade: 'B' },
// { id: '3', name: 'Charlie', score: 92, grade: 'A' }
// ]The mapRows() method is fully chainable with other configuration methods:
const result = csvToJson
.formatValueByType(true)
.fieldDelimiter(',')
.trimHeaderFieldWhiteSpace(true)
.mapRows(mapper)
.supportQuotedField(true)
.getJsonFromCsv('input.csv');The feature is also available in the browser API:
const convert = require('convert-csv-to-json');
const mapper = (row, index) => {
row.processed = true;
return row;
};
const json = await convert.browser
.mapRows(mapper)
.parseFile(fileInputElement.files[0]);- The mapper function is called for each data row (after the header row)
- Row indices are 0-based and only count data rows (header row is not included)
- Filtering with
nullreturns is more efficient than creating large intermediate arrays if you only need a subset of rows