![]() |
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 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.
Output eyJuYW1lIjoiR0ZHIiwiYWdlIjozMH0= 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.
Output eyJuYW1lIjoiR0ZHIiwiYWdlIjozMH0= |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |