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?

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63
  • 1
    Can you provide a link to the CSV library you're using? – Barmar Jun 02 '14 at 23:56
  • https://code.google.com/p/jquery-csv/ – Cute_Ninja Jun 02 '14 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. – Bergi Jun 02 '14 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. – Barmar Jun 03 '14 at 00: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. – Barmar Jun 03 '14 at 00:02
  • It works fine for me: http://jsfiddle.net/Mh8h7/2/ – Barmar Jun 03 '14 at 00:10
  • I found the issue. When i parse the file, the string is wrapped up in quotes (and that happens because it is string..duhh) so "__stream_code","project_code","process" becomes ""__stream_code","project_code","process"" whcih creates problem – Cute_Ninja Jun 03 '14 at 00:14
  • However, I still don't know the solution – Cute_Ninja Jun 03 '14 at 00:15
  • @Barmar Ahem! jquery-csv author here. jquery-csv is fully compliant with tests to prove it. It shouldn't have any issue with mixed quoted and unquoted data. Also, it it starts to parse and reaches bad data it'll indicate what row/column the error was encountered. – Evan Plaice Jan 21 '16 at 14:02
  • @EvanPlaice I said "that particular library seems OK" and "it works fine for me". – Barmar Jan 21 '16 at 16:28
  • @Barmar Gotcha, I didn't open the jsfiddle. – Evan Plaice Jan 22 '16 at 00:01

2 Answers2

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.

codebased
  • 6,945
  • 9
  • 50
  • 84
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

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94