Open In App

How to Convert Blob Data to JSON in JavaScript ?

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

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.

Using FileReader API

In this approach, we first use the readAsText() method to read the Blob as text and once the data is loaded we parse the text into JSON using JSON.parse().

Example: The below code implements the FileReader API to convert the blob data into JSON format.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        Converting Blob to JSON
    </title>
</head>

<body style="text-align: center;">
    <h2>
        Converting "{\"name\": \"John\", 
        \"age\": 30}" 
        <br/>string to Blob and then 
        formatting it into JSON
    </h2>
    <h3 id="json"></h3>
    <script>
        const jsonHeading = 
            document.getElementById('json');
        const blob = new Blob(
            ["{\"name\": \"John\", \"age\": 30}"], 
            { type: "application/json" });
        const reader = new FileReader();
        reader.onload = function (event) {
            const jsonData = 
                JSON.parse(event.target.result);
            jsonHeading.innerHTML = 
                "Converted JSON Data = { Name: " + 
                    jsonData.name + ", Age: " + 
                    jsonData.age + " }";
        };
        reader.readAsText(blob);

    </script>
</body>

</html>

Output:

blobOp

Using TextDecoder API

In this approach, we will use the decode() method of TextDecoder to decode the Blob data as text and once the data is decoded we parse the text into JSON using JSON.parse().

Example: The below code implements the TextDecoder API to convert blob data into JSON.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        Converting Blob to JSON
    </title>
</head>

<body style="text-align: center;">
    <h2>
        Converting "{\"name\": \"John\",
        \"age\": 30}"
        <br />string to Blob and then
        formatting it into JSON
    </h2>
    <h3 id="json"></h3>
    <script>
        const jsonHeading =
            document.getElementById('json');
        const blob = new Blob(
            ["{\"name\": \"John\", \"age\": 30}"], 
            { type: "application/json" });
        const reader = new FileReader();
        reader.onload = function (event) {
            const textDecoder = new TextDecoder();
            const decodedText = 
                textDecoder.decode(event.target.result);
            const jsonData = JSON.parse(decodedText);
            jsonHeading.innerHTML =
                "Converted JSON Data = { Name: " +
                jsonData.name + ", Age: " +
                jsonData.age + " }";
        };
        reader.readAsArrayBuffer(blob);

    </script>
</body>

</html>

Output:

blobOp



Previous Article
Next Article

Similar Reads

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. I
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
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
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
How to convert JSON data to a html table using JavaScript/jQuery ?
Given an HTML document containing JSON data and the task is to convert JSON data into an HTML table.  These are the following approaches for converting the JSON data to an HTML table: Table of Content Using for loopUsing JSON.stringify() methodApproach 1: Using for loopTake the JSON Object in a variable.Call a function that first adds the column na
4 min read