javascript - Download JSON object as a file from browser

Javascript - Download JSON object as a file from browser

To download a JSON object as a file from the browser, you can create a Blob object containing the JSON data, create a URL for the Blob object using the URL.createObjectURL() method, and then create a link element with the download attribute set to the desired filename and the href attribute set to the URL of the Blob object. Here's how you can do it:

function downloadJSON(jsonData, filename) { // Convert JSON object to string var jsonString = JSON.stringify(jsonData); // Create a new Blob object with the JSON data var blob = new Blob([jsonString], { type: 'application/json' }); // Create a URL for the Blob object var url = URL.createObjectURL(blob); // Create a link element var link = document.createElement('a'); // Set the download attribute to the filename link.setAttribute('download', filename); // Set the href attribute to the URL of the Blob object link.setAttribute('href', url); // Append the link to the document body document.body.appendChild(link); // Click the link to trigger the download link.click(); // Remove the link from the document body document.body.removeChild(link); } // Example usage: var jsonData = { "name": "John", "age": 30, "city": "New York" }; var filename = 'data.json'; downloadJSON(jsonData, filename); 

In this code:

  • We define a function downloadJSON that takes two parameters: the JSON object (jsonData) and the filename (filename) for the downloaded file.
  • Inside the function, we convert the JSON object to a string using JSON.stringify() to prepare it for download.
  • We create a Blob object containing the JSON data with the MIME type application/json.
  • We create a URL for the Blob object using URL.createObjectURL().
  • We create a link element (<a>) and set its download attribute to the filename and its href attribute to the URL of the Blob object.
  • We append the link to the document body, trigger a click on it to initiate the download, and then remove the link from the document body.

This will prompt the browser to download the JSON data as a file with the specified filename. Adjust the jsonData and filename variables as needed for your specific use case.

Examples

  1. "JavaScript save JSON object as file" Description: This query is about how to save a JSON object as a file using JavaScript in a web browser. Below is a code snippet demonstrating this functionality.

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'yourFileName.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); 
  2. "JavaScript export JSON to file" Description: This search query is about exporting JSON data to a file using JavaScript. Here's how you can do it:

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = 'data.json'; link.click(); 
  3. "JavaScript create and download JSON file" Description: This query relates to creating and downloading a JSON file dynamically using JavaScript. Below is a code snippet to achieve this:

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); 
  4. "JavaScript save JSON to file client side" Description: This query is about saving JSON data to a file client-side using JavaScript. Here's how you can accomplish it:

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); 
  5. "JavaScript download JSON file on button click" Description: This query focuses on downloading a JSON file when a button is clicked using JavaScript. Here's how you can implement it:

    <button onclick="downloadJSON()">Download JSON</button> <script> function downloadJSON() { const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); } </script> 
  6. "JavaScript export JSON to file on client side" Description: This search query seeks a method to export JSON data to a file on the client-side using JavaScript. Below is a code snippet demonstrating this:

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); 
  7. "JavaScript generate JSON file for download" Description: This query is about generating a JSON file dynamically for download using JavaScript. Below is a code snippet illustrating this:

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); 
  8. "JavaScript download JSON data as file" Description: This query pertains to downloading JSON data as a file using JavaScript. Below is a code snippet demonstrating how to achieve this:

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); 
  9. "JavaScript save JSON response as file" Description: This query focuses on saving a JSON response from an API as a file using JavaScript. Below is a code snippet demonstrating how to achieve this:

    fetch('yourApiEndpoint') .then(response => response.json()) .then(data => { const jsonContent = JSON.stringify(data); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); }); 
  10. "JavaScript create JSON file for download" Description: This query is about creating a JSON file dynamically for download using JavaScript. Below is a code snippet illustrating this:

    const jsonContent = JSON.stringify(yourJSONObject); const blob = new Blob([jsonContent], { type: 'application/json' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'data.json'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); 

More Tags

ksqldb powerbi-desktop pointer-arithmetic apache-commons-httpclient fuzzy-search azure-aks timelapse ms-access zappa csrf

More Programming Questions

More Auto Calculators

More Fitness-Health Calculators

More Investment Calculators

More Electrochemistry Calculators