The transform callback provides a way for your program to hook into the data stream and transform the data before it is written to the next stream. Please note the columns options, needed to keep the columns name in the JSON objects (otherwise you'll get a simple array) and the end options set to false, which tells node not to close the file stream when the CSV stream closes: this allows us to add the last ']'. Return (index = 0 ? '' : ',\n') + JSON.stringify(row) Var f = fs.createReadStream('Fielding.csv') Here is a sample application, which will write JSON objects to a file: var csv = require('csv') You'll need to install node.js and csv ( npm install csv). The npm csv package is able to process a CSV stream, without having to store the full file in memory. Please do let me know if you meet any problems next time so I could solve it in time. The purpose of the lib is to relief stress like this. I have manually tested it with a csv file over 3 million records and it works without an issue. Or if you want to stream data from another application: cat | csvtojson > converted.json You can the csvtojson library to quickly convert CSV to JSON in JavaScript: index.js import csvToJson from 'csvtojson' const csvFilePath 'data.csv' const json await csvToJson ().fromFile (csvFilePath) console.log (json) For a data.csv file like this: data. You can update it by running npm install -g csvtojsonĪfter you've installed latest csvtojson, you just need to run: csvtojson > converted.json Please make sure you have the latest csvtojson release. Please check out Process Big CSV File in Command Line. I also added some extra lines in README for your scenario. I am sorry it did not work out for you and it was caused by a bug solved several months ago. I understand that the initial rows in an Excel spreadsheet will convert to objects when parsed as JSON but what I am trying to achieve is converting to JSON with nested arrays.You mentioned csvtojson module above and that is an open source project which I am maintaining. To add to the above, I should have been clearer. I am currently just using an online CSV to JSON converter, however my problem is that I can't seem to figure out a way to format it so that it uses arrays of objects.ĭoes anyone know how to format in Excel to get the desired array of objects? Otherwise can someone point me in the right direction for a script that will convert it correctly? In Excel I have created a spreadsheet that I would like to convert into JSON for my VS Code project.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |