1

I have a csv as such:

this is the first column, this is the 2nd,
firstVal,                 secondVal
david,                    baseball
jon,                      soccer

I want to convert this to:

[{firstVal:david, secondVal:baseball},{firstVal:jon,secondVal:soccer}]

My first row in my csv is metadata (basically just a description of the actual column headers--firstVal and secondVal) that I don't want to be included in the json. I've tried:

csvtojson({noheader: true}).fromFile(csvFilePath)//...

but this doesn't seem to work. How can I do this conversion ignoring the first row?

yalpsid eman
  • 3,064
  • 6
  • 45
  • 71
  • Can you clarify your csv example? Are `this is the first column` and `this is the 2nd` your own annotations or the values of the first row? Is the issue that you need `firstVal` and `secondVal` to be treated as the headers despite being in the second row? What output are you actually getting at the moment? – Mickey Sep 06 '19 at 16:43

1 Answers1

4

My first row in my csv is metadata

Then the problem is with your CSV. Which is fine. You can still use it, but you can't expect a library to have the tools you'll need to account for this strange use case.

I think what makes the most sense is to read in the contents of the csv as a string and remove the first line before feeding it in to csvtojson. For that, I will borrow from this answer:

csvStr = csvStr.substring(csvStr.indexOf("\n") + 1);

Then you hand that over to csvtojson:

csv().fromString(csvStr)
CoryCoolguy
  • 1,065
  • 8
  • 18
  • While it's conventional to (optionally) treat the first line as column headings, it's not required — there is no formal specification of CSV, although [RFC-4180](https://tools.ietf.org/html/rfc4180) was created to codify existing practice. Spreadsheet programs will load the OP's sample with no problem; the CSV is _valid_, if unconventional. Either way this is an otherwise good answer. – Stephen P Sep 06 '19 at 19:17
  • I understand that csv is a *very* loose standard. The first line is either going to be the header or just data, typically. I've never heard of a CSV using the first line for metadata. Is that a thing? – CoryCoolguy Sep 06 '19 at 19:26
  • 1
    CSV is a very generic format which is not tied to spreadsheets, even though that's its biggest use. CSV has rules about quoting and escaping, but can be used for anything and it's up to a program to interpret the _meaning_, as long as it is well-formed. Think of a tab-separated-values (TSV) file which has no typical conventions associated with it; is it valid or invalid? What determines validity? ... OP has a tool which has the typical expectation that there is exactly zero or one header line, so has to deal with the "extra" line — but regardless, OP's CSV itself is well-formed. – Stephen P Sep 06 '19 at 19:40
  • And I would agree with that. It can be parsed, but without modification will only be parsed in a way that's not desirable. – CoryCoolguy Sep 06 '19 at 19:43
  • What is csvStr? There is only a csv file. How are you converting the file to a string? – yalpsid eman Sep 06 '19 at 20:05
  • 1
    @yalpsideman csvStr is just a varaible. Call it whatever you like and put the contents of `csvFilePath` in it. As for the act of actually reading the file, I'd recommend you look at using the `fs` library provided by node: [fs.readFile](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback). – CoryCoolguy Sep 06 '19 at 20:11