Skip to main content
100%

a simple function to export a JSON file

✓ Published0🌍 Public
Aamundo
Last edited Nov 22, 2014
Created on Nov 22, 2014

This example demonstrates a minimal JavaScript function that triggers a browser save dialog for any JSON data, producing a downloadable file with a user-specified name. The code converts the input data into a formatted string using `JSON.stringify`, wraps it in a Blob with the `application/json` MIME type, and creates an object URL via `URL.createObjectURL`. It then programmatically clicks a temporary anchor element with a `download` attribute and cleans up the element afterward. The function accepts arbitrary data, such as arrays or text, as shown in the comment example `saveJSON([1,2,3], 'anArray')`.

AI-generated description

Similar vizzes