0

I know it can be done in other languages like Powershell, I did a lot of searching but couldn't find any how it can be done in node.js or javascript.

For example let's say I have:

carModel,price,color
"Audi",10000,"blue"
"BMW",15000,"red"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"

and I want to append to line 3 something like:

carModel,price,color,errorcode,errormsg
"Audi",10000,"blue"
"BMW",15000,"red","05","wrong price"
"Mercedes",20000,"yellow"
"Porsche",30000,"green"

My question isn't really about how to solve the above, but:

Is there any way to manipulate a csv file in js directly without converting it to json objects and converting it back to csv?

  • *"without converting it to json objects"* You wouldn't convert it to *JSON* at all. You might well convert it to objects (specifically, arrays), but not JSON. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Jun 06 '18 at 10:30

1 Answers1

0

Is there any way to manipulate a csv file in js directly...

Sure, you could read it all into memory as one big string, then splice content into the string. It probably wouldn't be a great idea, though, as compared to at least reading it in as an array of strings or an array of arrays.

Also note that reading a CSV can be more work than it seems thanks to line breaks within quoted fields, etc., so you're probably best off using one of the CSV parser npm modules...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875