1

I am using jquery CSV library (https://github.com/evanplaice/jquery-csv/) , to parse and convert a CSV file into an array of objects. This is my code

this.parsedData = $.csv.toObjects(data);

if (this.parsedData.length === 0) {
**console.log('In valid'); /**/ This gets printed 
} else {
    console.log('valid')
}

My CSV file is this:

""__stream_code","project_code","process","due_date","root_table"
"DASH_PLAY_001","DEV","Plate",2013-02-02,"stream"
"DASH_PLAY_001","DEV","DepthAssess",2013-02-03,"stream""

Now, if I remove quotes, it works fine, but with quotes it doesn't. Most of the time, my application is going to handle CSV with values in quotes. Is there any way to fix it?

11
  • 1
    Can you provide a link to the CSV library you're using? Commented Jun 2, 2014 at 23:56
  • code.google.com/p/jquery-csv Commented Jun 2, 2014 at 23:57
  • 1
    Choose a better CSV library. There are many, and probably even other ones that allocate the jQuery.csv namespace. Edit: Altough that particular library seems OK, claiming to have implemented the RFC format. Commented Jun 2, 2014 at 23:58
  • 1
    Isn't your if condition backwards? If the CSV is valid, it will return an array of objects, so length will be non-zero. Commented Jun 3, 2014 at 0:01
  • Also, you can get zero if the CSV is valid but has no data rows. The documentation isn't clear, but my guess is it returns null or undefined if it gets an error, not a zero-length array. Commented Jun 3, 2014 at 0:02

2 Answers 2

1

Try with http://papaparse.com/; you can parse it online and it works great.

All you have to do is call through var results = $.parse(csvString);

and it will return the JSON object for you.

Sign up to request clarification or add additional context in comments.

Comments

0

Why is it wrapped in quotes?

I've tested it without the outer quotes on the Basic Usage demo and it seems to parse the data just fine. Are you using at least version 0.71?

This:

""__stream_code","project_code","process","due_date","root_table"
"DASH_PLAY_001","DEV","Plate",2013-02-02,"stream"
"DASH_PLAY_001","DEV","DepthAssess",2013-02-03,"stream""

Double-double quotes cancel each out (as per the spec) so this'll error when it tries to parse the first value.

Should be:

"__stream_code","project_code","process","due_date","root_table"
"DASH_PLAY_001","DEV","Plate",2013-02-02,"stream"
"DASH_PLAY_001","DEV","DepthAssess",2013-02-03,"stream"

If jquery-csv encounters an error during parsing it should log an error to the console indicating the row:column where the error occurred. Is there an error in the console. If not what does console.log(data) output?

As for the data being a mix of quoted/unquoted, the parser shouldn't have any issues consuming it. The data (sans outer quotes) looks perfectly valid.

BTW, I'm not trying to shed blame. If there is something else here that isn't in the code sample you provided that exposes a bug in the parser, I'd like to identify and fix it.

Disclaimer: I'm the author of jquery-csv

1 Comment

I think those surrounding quotes are just how console.log() displays strings.

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.