Skip to content

Instantly share code, notes, and snippets.

@nurfarazi
Created July 23, 2016 12:06
Show Gist options
  • Save nurfarazi/6967a2b91e87a74910be4f23b485bb4d to your computer and use it in GitHub Desktop.
Save nurfarazi/6967a2b91e87a74910be4f23b485bb4d to your computer and use it in GitHub Desktop.
function Json2CSV(objArray)
{
var
getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys.join();
}, array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray
, str = ''
;
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if(line != '') line += ','
line += array[i][index];
}
str += line + '\r\n';
}
str = getKeys(objArray[0]) + '\r\n' + str;
var a = document.createElement('a');
var blob = new Blob([str], {'type':'application\/octet-stream'});
a.href = window.URL.createObjectURL(blob);
a.download = 'exported.csv';
a.click();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment