Is there JSON encode/decode base64 encode/decode function in JavaScript?
Asked
Active
Viewed 7.2k times
25
-
possible duplicate of [How to base64 encode inside of javascript](http://stackoverflow.com/questions/3774622/how-to-base64-encode-inside-of-javascript). Searching is hard, apparently. http://stackoverflow.com/search?q=javascript+base64 – Matt Ball Jan 12 '11 at 03:28
-
take a look at: http://www.json.org/js.html – Alexar Sep 27 '11 at 03:19
5 Answers
17
Yes, btoa() and atob() work in some browsers:
var enc = btoa("this is some text");
alert(enc);
alert(atob(enc));

david
- 17,925
- 4
- 43
- 57
-
17
-
5
-
5
-
Why window.opera? I eliminated that and it works in Firefox and Chrome. Not sure why IE's tools don't let me test JavaScript in the script console thingy. I'll keep looking. – vbullinger Feb 13 '13 at 23:16
-
For reference its supported in all browsers since IE 10. https://caniuse.com/#feat=atob-btoa – Nisd Nov 01 '18 at 08:00
5
JSON and base64 are completely independent.
4
This might be helpful for you. Using a combination of this project crypto-js and Prototype to parse JSON I wrote two function to encode/decode JSON to Base 64. (These functions don't check for not well formatted json)
function JSONtoBase64(jsonObj) { return Crypto.util.bytesToBase64(Crypto.charenc.UTF8.stringToBytes(Object.toJSON(jsonObj))); }; function base64ToJSON(bytes) { var jsonString = Crypto.charenc.UTF8.bytesToString(Crypto.util.base64ToBytes(bytes)); return jsonString.evalJSON(); };

Diego Marafetti
- 465
- 3
- 7
2
For non-Mozilla browsers, use: http://www.webtoolkit.info/javascript-base64.html
For Mozilla browsers, use btoa()
and atob()
.

David Titarenco
- 32,662
- 13
- 66
- 111
0
I don't think there's one built in, but here's the functions for JSON in jquery: (can't post links since I'm new)
jQuery.getJSON
jQuery.parseJSON
and here's a link for base64 encoding in javascript.
http://www.webtoolkit.info/javascript-base64.html

Andy
- 2,263
- 2
- 14
- 4
-
jQuery.parseJSON has been added in 1.4 and does not work in previous versions. – Alexar Sep 26 '11 at 20:01