csvtojson node.js (combine two codes)

Viewed 224

How to combine these two codes, so it doesn't just covert csv to Json (first code), but also save this as an json array in an extra file?(second code) this (first) code converts csv file to json array:

const fs = require("fs");

let fileReadStream = fs.createReadStream("myCsvFile.csv");
let invalidLineCount = 0;

const csvtojson = require("csvtojson");
csvtojson({ "delimiter": ";", "fork": true })
.preFileLine((fileLineString, lineIdx)=> {
    let invalidLinePattern = /^['"].*[^"'];/;
    if (invalidLinePattern.test(fileLineString)) {
        console.log(`Line #${lineIdx + 1} is invalid, skipping:`, fileLineString);
        fileLineString = "";
        invalidLineCount++;
    }
    return fileLineString
})
.fromStream(fileReadStream) 
.subscribe((dataObj) => { 
    console.log(dataObj);
// I added the second code hier, but it wirtes the last object of the array (because of the loop?)
}    
});

and this (second) code saves the json array to an external file:

fs.writeFile('example.json', JSON.stringify(dataObj, null, 4);

The quistion is how to put the second codes into the first code (combine them)?

2 Answers

You can use .on('done',(error)=>{ ... }) method. (csvtojson). Push the data into a variable in subscribe method and write the data as JSON in .on('done'). (test was successful).

Check it out:

let fileReadStream = fs.createReadStream("username-password.csv");
let invalidLineCount = 0;
let data = []

csvtojson({ "delimiter": ";", "fork": true })
  .preFileLine((fileLineString, lineIdx)=> {
    let invalidLinePattern = /^['"].*[^"'];/;
    if (invalidLinePattern.test(fileLineString)) {
        console.log(`Line #${lineIdx + 1} is invalid, skipping:`, fileLineString);
        fileLineString = "";
        invalidLineCount++;
    }
    return fileLineString
  })
  .fromStream(fileReadStream) 
  .subscribe((dataObj) => {
    // console.log(dataObj)
    data.push(dataObj)
  })
  .on('done',(error)=>{
    fs.writeFileSync('example.json', JSON.stringify(data, null, 4))
  })

Not sure if you are able to change the library but I would definitely recommend Papaparse for this - https://www.npmjs.com/package/papaparse

Your code would then look something like this:

const fs = require('fs'), papa = require('papaparse');
  var readFile = fs.createReadStream(file);
        papa.parse(readFile, {
            complete: function (results, file) {
                fs.writeFile('example.json', JSON.stringifiy(results.data), function (err) {
                  if(err) console.log(err);
                  // callback etc
                })
            }
        });
Related