Created
May 10, 2025 14:59
-
-
Save senthilmpro/bbd9af0a5a9bc8b238d6b1b6fda6c457 to your computer and use it in GitHub Desktop.
download-json-text-as-file-browser
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
const downloadJsonAsFile = (exportObj, fileName) => { | |
let outputName = fileName || 'download.json'; | |
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj, null, 2)); | |
var downloadAnchorNode = document.createElement('a'); | |
downloadAnchorNode.setAttribute("href", dataStr); | |
downloadAnchorNode.setAttribute("download", outputName); | |
document.body.appendChild(downloadAnchorNode); // required for firefox | |
downloadAnchorNode.click(); | |
downloadAnchorNode.remove(); | |
} | |
//let sampleObj = {"key": "value"}; | |
// downloadJsonAsFile(JSON.stringify(sampleObj), "sample.json"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment