Skip to content

Instantly share code, notes, and snippets.

@rudyhuynh
Last active July 7, 2021 04:10
Show Gist options
  • Save rudyhuynh/9ef7a77671b21dba93dd3509bda44929 to your computer and use it in GitHub Desktop.
Save rudyhuynh/9ef7a77671b21dba93dd3509bda44929 to your computer and use it in GitHub Desktop.
Download a JSON in javascript
function downloadJson(data, filename = 'data.json') {
const dataAtr =
'data:text/json;charset=utf-8,' +
encodeURIComponent(JSON.stringify(data));
const el = window.document.createElement('a');
el.setAttribute('href', dataAtr);
el.setAttribute('download', filename);
el.click();
}
// usage:
downloadJson({name: 'X'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment