Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
csvtojson node.js (combine two codes)

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)?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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))
  })
about 4 years ago · Juan Pablo Isaza Report

0

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
                })
            }
        });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!