How to Convert JSON to base64 in JavaScript ? Last Updated : 16 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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 JSON object. First, the JSON data is converted to a UTF-8 string using unescape and encodeURIComponent, and then btoa encodes this UTF-8 string to Base64. Syntax:const base64String = btoa(stringToEncode);Example: The below example uses btoa function to convert JSON to Base64 in JavaScript. JavaScript const jData = { name: 'GFG', age: 30 }; const utf8Str = unescape(encodeURIComponent (JSON.stringify(jData))); const res = btoa(utf8Str); console.log(res); OutputeyJuYW1lIjoiR0ZHIiwiYWdlIjozMH0= Using Manual ConversionIn this approach, we're manually converting a JSON string to Base64. The function conversionFn encodes the UTF-16 characters of the JSON string into Base64 using a custom algorithm that follows the Base64 encoding table, including handling padding characters ('='). Example: The below example uses Manual Conversion to convert JSON to Base64 in JavaScript. JavaScript const jData = { name: 'GFG', age: 30 }; const jStr = JSON.stringify(jData); const baseTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; function conversionFn(str) { let res = ''; let i = 0; while (i < str.length) { const c1 = str.charCodeAt(i++); const c2 = str.charCodeAt(i++); const c3 = str.charCodeAt(i++); const triplet = (c1 << 16) | (c2 << 8) | c3; const b64c1 = baseTable. charAt((triplet >>> 18) & 0x3F); const b64c2 = baseTable. charAt((triplet >>> 12) & 0x3F); const b64c3 = baseTable. charAt((triplet >>> 6) & 0x3F); const b64c4 = baseTable. charAt(triplet & 0x3F); res += b64c1 + b64c2 + b64c3 + b64c4; } const padding = str.length % 3; if (padding === 1) { res = res.slice(0, -2) + '=='; } else if (padding === 2) { res = res.slice(0, -1) + '='; } return res; } const res = conversionFn(jStr); console.log(res); OutputeyJuYW1lIjoiR0ZHIiwiYWdlIjozMH0= Comment More infoAdvertise with us Next Article How to Convert JSON to base64 in JavaScript ? G gpancomputer Follow Improve Article Tags : JavaScript Web Technologies JSON Similar Reads How To Convert Base64 to JSON String in JavaScript? 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 se 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 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 Like