Last active
July 7, 2021 04:10
-
-
Save rudyhuynh/9ef7a77671b21dba93dd3509bda44929 to your computer and use it in GitHub Desktop.
Download a JSON in javascript
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 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