1

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!

2 Answers 2

3

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

I found that this is a much simpler and faster approach. Thanks!
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.