I need to 1) read 2) write to 3) filter out columns with a small CSV, this guide is for you!

tl;dr: The final repo is here: https://github.com/teamzerolabs/node-csv-example.

Quick and Dirty — Convert CSV into JSON

yarn add neat-csv

Load the csv string with fs and fs.readFileSync

async function readCSV(content) {

const result = await neatCsv(content);

return result;

} //

// Result would be => [

...

{ street: '6236 LONGFORD DR Unit 1',

city: 'CITRUS HEIGHTS',

zip: '95621',

state: 'CA',

beds: '2',

baths: '1',

sq__ft: '795',

type: 'Condo',

sale_date: 'Wed May 21 00:00:00 EDT 2008',

price: '116250',

latitude: '38.679776',

longitude: '-121.314089' },

{ street: '250 PERALTA AVE',

city: 'SACRAMENTO',

zip: '95833',

state: 'CA',

beds: '2',

baths: '1',

sq__ft: '588',

type: 'Residential',

sale_date: 'Wed May 21 00:00:00 EDT 2008',

price: '120000',

latitude: '38.612099',

longitude: '-121.469095' },

...

]

Now you can transform it with result.forEach/lodash/do other type of cleanups like string to number or date conversion.

Convert JSON back to CSV string

yarn add json2csv

Take the existing array and specify the following:

const { parseAsync } = require("json2csv"); // Transform fields from string to numbers first.

result.forEach(record => {

record.zip = parseFloat(record.zip);

record.sq__ft = parseFloat(record.sq__ft);

record.baths = parseFloat(record.baths);

record.beds = parseFloat(record.beds);

record.price = parseFloat(record.price);

record.latitude = parseFloat(record.latitude);

record.longitude = parseFloat(record.longitude);

}); // Specify fields (columns to keep), and we are ready to convert. const fields = [

"zip",

"sq__ft",

"price",

... and any field we want to keep

];

const opts = { fields };



const resultCSV = await parseAsync(result, opts);

fs.writeFileSync("./cleanedup.csv", resultCSV); // ./cleanedup.csv contains: "zip","sq__ft","price"

95838,836,59222

95823,1167,68212

95815,796,68880

95815,852,69307

95824,797,81900

95841,1122,89921

95842,1104,90895

95820,1177,91002

95670,941,94905

95673,1146,98937

95838,909,100309

95823,1289,106250

95815,871,106852

95822,1020,107502

95842,1022,108750

95842,1134,110700

More References

https://mircozeiss.com/json2csv/

https://github.com/sindresorhus/neat-csv

If you need streaming support instead of promises, neat-csv sits on top of csv-parser

We will cover how to work with Excel Files next time! Feel free to reach out if you get stuck somewhere with the example.