Forked from Sikwan/Javascript: Json to Download CSV
Last active
August 29, 2015 14:07
-
-
Save charlesponti/f59b6a94c3b3713b4ed8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Json2CSV(objArray) | |
{ | |
var | |
getKeys = function(obj){ | |
var keys = []; | |
for(var key in obj){ | |
keys.push(key); | |
} | |
return keys.join(); | |
}, objArray = format_json(objArray) | |
, 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 = 'export.csv'; | |
a.click(); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment