All rows from the CSV file are converted to JSON objects that are added to the resulting array, which is then converted to JSON, and a corresponding JSON output file is generated. You can modify the code above to allow for varying and more complex CSV data. Convert CSV to JSON in JavaScript In this approach, we put the contents of the CSV file into an array and split the contents of the array based on a delimiter. Index.js import csvToJson from 'csvtojson' const csvFilePath = 'data.csv' const json = await csvToJson().fromFile(csvFilePath) console.log(json) ĭata.csv color,maxSpeed,age "red",120,2 "blue",100,3 "green",130,2 There are 1391 other projects in the npm registry using json2csv. Start using json2csv in your project by running npm i json2csv. For instance, we can get a CSV from a file and convert it to JSON as follows: person.csv firstname,lastnamejohn,smithjane,smith index.js const csvFilePath person. You can the csvtojson library to quickly convert CSV to JSON in JavaScript: Latest version: 6.0.0-alpha.2, last published: 2 months ago.
0 Comments
Leave a Reply. |