Skip to content

Instantly share code, notes, and snippets.

@amundo
Created November 22, 2014 01:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amundo/3951b04c1e0725445774 to your computer and use it in GitHub Desktop.
Save amundo/3951b04c1e0725445774 to your computer and use it in GitHub Desktop.
a simple function to export a JSON file
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