How to use JSON.stringify and Blob constructor In Javascript
This approach involves stringifying the JSON object into a human-readable format and then creating a Blob object from the string.
Example: This example shows the use of the above-explained approach.
const {Blob} = require('buffer');
const jsonData = { name: "Example Data", value: 10 };
// Stringify the JSON object
const jsonString = JSON.stringify(jsonData);
// Create a Blob object with the JSON string and
// set the content type as "application/json"
const blob =
new Blob([jsonString], { type: "application/json" });
// Blob object representing the JSON data
console.log(blob);
Output
Blob { size: 34, type: 'application/json' }
How to Convert JSON to Blob in JavaScript ?
This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data.
What is JSON and Blob?
- JSON (JavaScript Object Notation): A lightweight format for storing and transmitting data. It uses key-value pairs to represent structured data.
- Blob (Binary Large Object): A Blob represents a raw data object that can hold various types of data, including text, images, or even audio.
Below are the methods for converting JSON data to a Blob object:
Table of Content
- Using JSON.stringify and Blob constructor
- Using fetch API (for pre-existing JSON data)