Closed grisanu closed 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
})
@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
})
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.
Considering a new header
event which will be populated when header:true
I would use that header
event today if it was there 👍
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!
@Keyang, any status?
Added pull request #192 that emits the "header"
event.
Is there a way to get the header row from a
csv
event (without listening tojson
,end_parsed
orrecord_parsed
)? The following does not provide access to header row fromcsv
event: