export data in json format in javascript
"use strict"; function exportToJsonFile(jsonData) { let dataStr = JSON.stringify(jsonData); let dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(dataStr); let exportFileDefaultName = 'data.json'; let linkElement = document.createElement('a'); linkElement.setAttribute('href', dataUri); linkElement.setAttribute('download', exportFileDefaultName); linkElement.click(); }
Here is what the above code is Doing:
1. We create a function called exportToJsonFile.
2. We pass in the jsonData that we want to export.
3. We convert the jsonData to a string.
4. We create a data URI.
5. We create a link element.
6. We set the href attribute to the data URI.
7. We set the download attribute to the file name.
8. We click the link element.