Base64 encode a javascript object
Solution 1
You misunderstood the Buffer(str, [encoding])
constructor, the encoding
tells the constructor what encoding was used to create str
, or what encoding the constructor should use to decode str
into a byte array.
Basically the Buffer
class represents byte streams, it's only when you convert it from/to strings that encoding comes into context.
You should instead use buffer.toString("base64")
to get base-64 encoded of the buffer content.
let objJsonStr = JSON.stringify(obj);
let objJsonB64 = Buffer.from(objJsonStr).toString("base64");
Solution 2
From String to Base-64
var obj = {a: 'a', b: 'b'};
var encoded = btoa(JSON.stringify(obj))
To decode back to actual
var actual = JSON.parse(atob(encoded))
For reference look here.
https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
Solution 3
When converting object to base64 I was getting out of latin range issues and character invalid error.
I made it work in my project with the below line.
Include the base64
and utf8
node packages and access them like this:
var bytes = base64.encode(utf8.encode(JSON.stringify(getOverviewComments())));
Related videos on Youtube
johni
Updated on July 21, 2022Comments
-
johni almost 2 years
I have large Javascript objects which I would like to encode to base-64 for AWS Kinesis` It turns out that:
let objStr = new Buffer(JSON.stringify(obj), 'ascii'); new Buffer(objStr, 'base64').toString('ascii') !== objStr
I'm trying to keep this as simple as possible.
How can I base-64 encode JSON and safely decode it back to its original value?
-
xiaofeng.li almost 8 yearsWhy would you expect them equal?
-
johni almost 8 yearsWell, I'm trying to do with
base-64
whatJSON.parse(JSON.stringify(obj))
does on objects. How can I do that? -
David Ehrmann almost 8 yearsWould url-encoding work, too? The resultant string might be smaller than if it's base 64-encoded.
-
-
johni almost 8 yearsThanks for the explanation. Regarding your example - it does not work on large JSONs. I've just checked that, the decode returns only partial of the original JSON.
-
xiaofeng.li almost 8 yearsHow big is your JSON? I tried some large ones and it's working fine.
-
johni almost 8 yearsYep, you're right. I probably selected only part of the encoded string.
-
Cinn over 6 years
-
Kugel almost 6 yearsHave you tried this with non ASCII characters? atob and btoa are quite broken.
-
Bender over 4 yearsthis won't work with not escaped charecters in JSON
-
roshnet almost 4 years@Bender can you please give an example?
-
Mo. over 2 years@Kugel Any other solution available ?