Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON encode/decode base64 encode/decode in JavaScript

Tags:

Is there JSON encode/decode base64 encode/decode function in JavaScript?

like image 775
theHack Avatar asked Jan 12 '11 03:01

theHack


People also ask

Can you base64 encode JSON?

Base64 is a safe encoding for JSON.

What is BTOA in JavaScript?

btoa() The btoa() method creates a Base64-encoded ASCII string from a binary string (i.e., a string in which each character in the string is treated as a byte of binary data).

What is encoding and decoding in JavaScript?

In JavaScript there are two functions respectively for decoding and encoding Base64 strings: btoa() : creates a Base64-encoded ASCII string from a "string" of binary data ("btoa" should be read as "binary to ASCII"). atob() : decodes a Base64-encoded string ("atob" should be read as "ASCII to binary").

Why is BTOA deprecated?

btoa(): accepts a string where each character represents an 8bit byte. If you pass a string containing characters that cannot be represented in 8 bits, it will probably break. Probably that's why btoa is deprecated.


2 Answers

Yes, btoa() and atob() work in some browsers:

var enc = btoa("this is some text"); alert(enc); alert(atob(enc)); 
like image 112
david Avatar answered Sep 24 '22 06:09

david


JSON and base64 are completely independent.

Here's a JSON stringifier/parser (and direct GitHub link).

Here's a base64 Q&A. Here's another one.

like image 26
Matt Ball Avatar answered Sep 24 '22 06:09

Matt Ball