Skip to content

Instantly share code, notes, and snippets.

@mojowen
Created January 15, 2013 01:12
Show Gist options
  • Save mojowen/4535115 to your computer and use it in GitHub Desktop.
Save mojowen/4535115 to your computer and use it in GitHub Desktop.
Importing data from Salsa's export system - using a csvImport function
// Use a simple jQuery GET request to get the data from export and return it to myQuery variable
$.get(
'http://hq-salsa.wiredforchange.com/dia/hq/export.jsp?',
{ 'query_KEY': 196951, 'type':'csv','include':'supporter_KEY,First_Name,Last_Name,Email'},
function(result) { myQuery = csvImport( result ); }
)
// Here's a function to parse the CSV and return the results as objects - using the first row as a name sequence
function csvImport( blob ) {
var rows = blob.trim().split("\n"),
results = [ ],
keys = [];
for( var i = 0; i < rows.length; i ++ ) {
var row = rows[i].split(','),
result = {} // The object we'll return for this row
for( var j = 0; j < row.length; j++) {
if( i == 0 && row[j].length > 2 ) { keys.push( row[j] ) } // Setting up the naming key
if( i > 0 && row[j].length > 2 ) { // Creating the objects
var value = row[j].slice(1,-1);
value = isNaN( parseFloat( value ) ) ? value : parseFloat( value ); // If it's number - return it as a float
result[ keys[j] ] = value;
}
}
if( i > 0 ) results.push( result ); // Push the result to the array to return
}
return results;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment