Reputation: 974
I have a node application which can end up with a CSV formatted table in a variable.
I'd like to parse it into a JSON style format.
All of the modules I've looked at seem to only take input from files (unless I'm misunderstanding)
Are there any modules out there which do what I'd like or would I be better off just rolling my own solution?
Thanks!
Upvotes: 1
Views: 1574
Reputation: 39385
As an alternative, you could use string.js. It works on Node.js and client side in the browser. Disclaimer: I'm the author.
Example:
var S = require('string');
S(myCsvData).lines().forEach(function(line){
var fields = S(line).parseCSV();
});
Upvotes: 3
Reputation: 54780
This example from NodeCSV seems to be parsing from a variable:
https://github.com/wdavidw/node-csv-parser
var csv = require('csv');
csv()
.from( '"1","2","3","4"\n"a","b","c","d"' )
.to( console.log )
In general, Node.js lets you treat both files and strings as streams. It's usually pretty trivial to go from one to the other.
Upvotes: 2