Nacho
Nacho

Reputation: 89

node csvtojson. Where is the result?

Great library to parse csv files!

Quick question: where is the result json stored after parsing a csv file? The tutorial at https://www.npmjs.com/package/csvtojson#library has this example:

const csvFilePath='<path to csv file>'

const csv=require('csvtojson')
csv()
.fromFile(csvFilePath)
.on('json',(jsonObj)=>{
    // combine csv header row and csv line to a json object 
    // jsonObj.a ==> 1 or 4 
})
.on('done',(error)=>{
    console.log('end')
})

But, once 'done' is called... where is my result json?!

I've inspected the object in the debugger and can't find the result.

Thanks in advance

Upvotes: 0

Views: 1501

Answers (1)

David R
David R

Reputation: 15637

Well, The documentation is kinda misleading.

I got it working by tweaking your code like this,

const csvFilePath='<path to csv file>'
const csv=require('csvtojson')

csv().fromFile(csvFilePath,function(err,result){

    if(err){
        console.log("An Error Has Occured");
        console.log(err);  
    } 

    var json = result;
    console.log(json);
});

Hope this helps!

Upvotes: 1

Related Questions