Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

324
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda