The Wayback Machine - https://web.archive.org/web/20240622222122/https://www.geeksforgeeks.org/how-to-convert-json-to-blob-in-javascript/
Open In App

How to Convert JSON to Blob in JavaScript ?

Last Updated : 18 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Using JSON.stringify and Blob constructor

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.

JavaScript
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' }

Using fetch API (for pre-existing JSON data)

If you already have the JSON data as a URL, you can use the fetch API to retrieve it and then convert it to a Blob object.

Example: This example shows the use of the above-explained approach.

JavaScript
const jsonUrl = "https://example.com/data.json";

fetch(jsonUrl)
  .then(response => response.blob())
  .then(blob => {
    console.log(blob); // Blob object containing the downloaded JSON data
  })
  .catch(error => console.error(error));
}


Choosing the Right Method:

  • Use the first method if you have the JSON data as a JavaScript object.
  • Use the second method if you have the JSON data stored remotely as a file accessible through a URL.

Conclusion:

By following these methods, you can effectively convert JSON data into Blobs for further processing or download in your JavaScript applications. Remember to specify the appropriate content type (“application/json”) when creating the Blob to ensure correct data interpretation.


Similar Reads

How to Convert Blob Data to JSON in JavaScript ?
When dealing with Blob data in JavaScript, such as binary data or files, we may need to convert it into JSON format for doing so JavaScript provides us with various methods as listed below. Table of Content Using FileReader APIUsing TextDecoder APIUsing FileReader APIIn this approach, we first use the readAsText() method to read the Blob as text an
2 min read
How to convert blob to base64 encoding using JavaScript ?
Blob is a fundamental data type in JavaScript. Blob stands for Binary Large Object and it is a representation of bytes of data. Web browsers support the Blob data type for holding data. Blob is the underlying data structure for the File object and the FileReader API. Blob has a specific size and file type just like ordinary files and it can be stor
2 min read
Convert JSON String to Array of JSON Objects in JavaScript
Given a JSON string, the task is to convert it into an array of JSON objects in JavaScript. This array will contain the values derived from the JSON string using JavaScript. There are two approaches to solve this problem, which are discussed below: Approach 1: Using JSON.parse() MethodThe basic method to convert JSON String to Array of JSON object
2 min read
JavaScript | Blob
A blob object is simply a group of bytes that holds the data stored in a file. It may seem like that a blob is a reference to the actual file but actually it is not. A blob has its size and MIME just like that of a simple file. The blob data is stored in the memory or filesystem of a user depending on the browser features and size of the blob. A si
3 min read
How can JavaScript upload a blob ?
There are many ways to upload a blob (a group of bytes that holds the data stored in a file) in JavaScript, using XMLHttpRequest, Fetch API, jQuery. In this tutorial, we will discuss the two most common ways that are supported by a majority of the browsers. Note: To test our HTTP request, you can use minimal express server. You can also use any onl
2 min read
What is a Blob Object in JavaScript ?
In JavaScript, a Blob (Binary Large Object) is an object that represents raw binary data(collection of bytes). It is commonly used to handle and manipulate binary data, such as images, audio, video, or other types of files. The Blob object allows you to create, modify, and manipulate binary data in a format that can be easily used with various web
2 min read
How to parse JSON object using JSON.stringify() in JavaScript ?
In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: JSON.stringify(object, replacer, space); Paramete
2 min read
What is difference between JSON.parse() and JSON.stringify() Methods in JavaScript ?
JSON.parse() converts JSON strings to JavaScript objects, while JSON.stringify() converts JavaScript objects to JSON strings. JavaScript utilizes JSON for data interchange between servers and web pages. These methods enable easy data manipulation and transport in web development. JSON.parse() MethodJSON.parse() converts a JSON string to a JavaScrip
2 min read
JSON Modify an Array Value of a JSON Object
The arrays in JSON (JavaScript Object Notation) are similar to arrays in JavaScript. Arrays in JSON can have values of the following types: nullbooleannumberstringarrayobject The arrays in JavaScript can have all these but it can also have other valid JavaScript expressions which are not allowed in JSON. The array value of a JSON object can be modi
2 min read
How to use cURL to Get JSON Data and Decode JSON Data in PHP ?
In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP. cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more. Approach: We are going to fetch JSON data from one of free website,
2 min read
three90RightbarBannerImg