Convert csv to JSON with comments at the top of document (Node.js)
You can try replacing the lines before parsing (not tested) :
const jsonArray = await csv({delimiter:[";"]}).fromFile("file.csv")
.preRawData(csvRawData => csvRawData.replace(/^#.*\n/gm, ''));
Trying to convert CSV file to JSON in nodejs gives syntax error while just requiring the csv file
I think your csv file is not correcly formated, what is the name of your nodejs file, and can you give us the error line 14 please ?
EDIT :
you are probably using an old version of Node.js. If this is the case, either const csv = require('csv-parser');
to const csv = require('csv-parser/lib/es5')
;`
Or
the csv file which is incorrect. Give the path directly on createreadstream
NodeJS - Convert CSV to JSON Object array
You can use plain javascript, with split
and map
functions
var res = {};
res.fruits = 'apples|1,oranges|2,grapes|3,peach|4,pineapple|5' .split(',').map(e => ({ "name": e.split('|')[0], "value": e.split('|')[1] }));
document.write('<pre>' + JSON.stringify(res, 0, 2) + '</pre>');
Related Topics
How to Replace a Regex Substring Match in JavaScript
How Dangerous Is It in JavaScript, Really, to Assume Undefined Is Not Overwritten
Case Insensitive Jquery Attribute Selector
Reactjs Syntheticevent Stoppropagation() Only Works with React Events
Array Like Objects in JavaScript
Microsecond Timing in JavaScript
How to Access Dom Elements in Electron
Get Decimal Portion of a Number with JavaScript
Regex for Number with Decimals and Thousand Separator
React React-Router-Dom Pass Props to Component
What Is Array Literal Notation in JavaScript and When Should You Use It
Passing Python Data to JavaScript via Django
How to Select a Value in a Select Dropdown with JavaScript
How to Set the Id Attribute of a HTML Element Dynamically with Angularjs (1.X)
[Vue Warn]: Property or Method Is Not Defined on the Instance But Referenced During Render