How to Convert CSV to JSON in Node.Js

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



Leave a reply



Submit