How To Convert Base64 to JSON String in JavaScript? Last Updated : 13 Sep, 2024 Comments Improve Suggest changes Like Article Like Report There could be situations in web applications, where there is a need to decode the data from Base64 format back into its original JSON format. It generally happens when one has to transmit data over the network where Base64 encoding is well suited for encoding binary data.In this article, we will see how one can convert Base64 strings into JSON-formatted strings in JavaScript.Different approaches to convert Base64 to JSON String in JavaScript.Table of ContentUsing atob() and JSON.parse()Using Buffer in Node.jsApproach 1: Using atob() and JSON.parse()There are occasions, especially within web browsers when Base64 data and JSON have to be decoded and parsed separately.In the first step, we will use atob() function which converts Base64 string into a plain text string.In the last step, we will use JSON.parse() for conversion of the decoded string into a a JSON object. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Convert Base64 to JSON String</title> </head> <body> <h1>GeeksForGeeks</h1> <h3>Base64 to JSON Conversion</h3> <button id="convert-btn">Convert Base64 to JSON</button> <pre id="result"></pre> <script> // Sample Base64-encoded JSON string const base64Data = "eyAiTmFtZSI6ICJQYW5rYWogQmluZCIsICJBZ2UiOiAyMSwgIkNvdW50cnkiOiAiSW5kaWEiIH0="; // Function to convert Base64 to JSON function convertBase64ToJson() { try { // Decode Base64 to string const jsonString = atob(base64Data); // Parse string to JSON const jsonObject = JSON.parse(jsonString); // Display the JSON object document.getElementById("result").textContent = JSON.stringify(jsonObject, null, 2); } catch (error) { console.error("Error decoding Base64:", error); } } // Event listener for the convert button document.getElementById("convert-btn").addEventListener("click", convertBase64ToJson); </script> </body> </html> Outputatob() and JSON.parse()Approach 2: Using Buffer in Node.jsThis approach is adequate for JavaScript running on a server (Node.js) and where one has to deal with Binary and Base64 encoding.In first step you have to create a Buffer from Base64 string using Buffer.from(base64Data, ‘base64’). Use buffer.toString(‘utf-8’) and change the Buffer to a UTF-8 string.In last step we will use JSON.parse() for converting string into JSON object. JavaScript // Node.js Example // Sample Base64-encoded JSON string const base64Data = "eyAiTmFtZSI6ICJQYW5rYWogQmluZCIsICJBZ2UiOiAyMSwgIkNvdW50cnkiOiAiSW5kaWEiIH0="; // Convert Base64 to buffer const buffer = Buffer.from(base64Data, 'base64'); // Convert buffer to string and parse to JSON const jsonString = buffer.toString('utf-8'); const jsonObject = JSON.parse(jsonString); console.log(jsonObject); Output{ Name: 'Pankaj Bind', Age: 21, Country: 'India' } Comment More infoAdvertise with us Next Article How To Convert Base64 to JSON String in JavaScript? pankajbind Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Convert JSON to base64 in JavaScript ? Base 64 is the encoding scheme that represents binary data in a printable ASCII format, commonly used for data serialization and transmission. Table of Content Using btoa functionUsing Manual ConversionUsing btoa functionIn this approach, we're using btoa to encode a UTF-8 string representation of a 2 min read How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used 3 min read How to Convert String to JSON in JavaScript? In JavaScript, converting a string to JSON is important for handling data interchangeably between server and client, parsing external API responses, and storing structured data in applications. Below are the approaches to converting string to JSON in JavaScript: Table of Content Using JSON.parse()Us 2 min read How to Convert a Map to JSON String in JavaScript ? A Map is a collection of key-value pairs, where each key is unique. In this article, we will see how to convert a Map to a JSON (JavaScript Object Notation) string in JavaScript. However, JSON.stringify() does not directly support Map objects. Table of ContentUsing Object.fromEntries() MethodUsing A 2 min read 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 light 2 min read How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i 3 min read How to Convert Base64 to File in JavaScript? In web development, Base64 encoding is often used to represent binary data, such as images or files, with a string of ASCII characters, sometimes you may be required to change this Base64 string back into a file for instance for file uploads, downloads, or processing in the browser, this article aim 2 min read How to Convert Base64 to Blob in JavaScript? Working with files and data in web applications often involves dealing with binary data. One common scenario is converting a Base64 string into a Blob object, which can then be used in various ways, such as creating downloadable files or uploading images to a server. This article will guide you thro 4 min read How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert 3 min read How to convert image into base64 string using JavaScript ? In this article, we will convert an image into a base64 string using Javascript. The below approaches show the methods to convert an image into a base64 string using Javascript.ApproachHere we will create a gfg.js file which will include JavaScript code and one gfg.html file.Now we will put onchange 2 min read Like