Keyang / node-csvtojson

Blazing fast and Comprehensive CSV Parser for Node.JS / Browser / Command Line.
MIT License
2.02k stars 271 forks source link

Getting header row from `csv` event #141

Closed grisanu closed 7 years ago

grisanu commented 7 years ago

Is there a way to get the header row from a csv event (without listening to json, end_parsed or record_parsed)? The following does not provide access to header row from csv event:

/**
csvStr:
a,b,c
1,2,3
4,5,6
7,8,9
*/
const csv=require('csvtojson')
csv()
.fromString(csvStr)
.on('csv',(csvRow)=>{ // this func will be called 3 times 
    console.log(csvRow) // => [1,2,3] , [4,5,6]  , [7,8,9] 
})
.on('done',()=>{
    //parsing finished 
})
Keyang commented 7 years ago

Hi, you need pass in noHeader:true so like below:

const csv=require('csvtojson')
csv({noHeader:true})
.fromString(csvStr)
.on('csv',(csvRow)=>{ // this func will be called 3 times 
    console.log(csvRow) // => [1,2,3] , [4,5,6]  , [7,8,9] 
})
.on('done',()=>{
    //parsing finished 
})
bparise commented 7 years ago

@Keyang The example above, there is no way to get the headers when in csv mode. I need the headers of the CSV file but don't need each row mapped with them:

const csv=require('csvtojson')
csv()
    .fromString(csvStr)
    .on('header', (headers) => {}) // headers = [a, b, c]
    .on('csv',(csvRow)=>{ // this func will be called 3 times 
    console.log(csvRow) // => [1,2,3] , [4,5,6]  , [7,8,9] 
})
.on('done',()=>{
    //parsing finished 
})
Keyang commented 7 years ago

If your pass in noHeader:true in param, csv event will emit the header with first callback

Keyang

Sent from my iPhone

On 7 Mar 2017, at 20:02, Brandon Parise notifications@github.com wrote:

@Keyang The example above, there is no way to get the headers when in csv mode. I need the headers of the CSV file but don't need each row mapped with them:

const csv=require('csvtojson') csv() .fromString(csvStr) .on('header', (headers) => {}) // headers = [col1, col2,...] .on('csv',(csvRow)=>{ // this func will be called 3 times console.log(csvRow) // => [1,2,3] , [4,5,6] , [7,8,9] }) .on('done',()=>{ //parsing finished }) — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

Keyang commented 7 years ago

Considering a new header event which will be populated when header:true

ronkorving commented 7 years ago

I would use that header event today if it was there 👍

brucejo75 commented 7 years ago

bump

Here is my scenario:

I load my data with headers:true and I count on csvtojson to interpret my cells to form sub objects. for example:

sample1.csv

a,
{"foo":"bar"}

produces:

{"a.foo": "bar"}

sample2.csv

a,
{"bar":"bar"}

produces:

{"a.bar": "foo"}

all good...

But now I want to verify that headers are the same between these 2 csv files which they are. Unfortunately, all I can determine is I have 2 objects a.foo and a.bar. I cannot assume their header value for validation.

I would value the header event when header:true.

Thanks for the awesome package!

brucejo75 commented 7 years ago

@Keyang, any status?

brucejo75 commented 7 years ago

Added pull request #192 that emits the "header" event.