Created
November 22, 2014 01:44
-
-
Save amundo/3951b04c1e0725445774 to your computer and use it in GitHub Desktop.
a simple function to export a JSON file
This file contains 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 saveJSON(data, saveAs){ | |
var stringified = JSON.stringify(data, null, 2); | |
var blob = new Blob([stringified], {type: "application/json"}); | |
var url = URL.createObjectURL(blob); | |
var a = document.createElement('a'); | |
a.download = saveAs + '.json'; | |
a.href = url; | |
a.id = saveAs; | |
document.body.appendChild(a); | |
a.click(); | |
document.querySelector('#' + a.id).remove(); | |
} | |
/* | |
running saveJSON([1,2,3], 'anArray'); | |
will pop up a save dialog to save a file called anArray.json which contains [1,2,3]. | |
You can pass in whatever (like a text, a corpus, etc). | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment