Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda