Base64 encode a javascript object

129,144

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())));
Share:
129,144

Related videos on Youtube

johni
Author by

johni

Updated on July 21, 2022

Comments

  • johni
    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
      xiaofeng.li almost 8 years
      Why would you expect them equal?
    • johni
      johni almost 8 years
      Well, I'm trying to do with base-64 what JSON.parse(JSON.stringify(obj)) does on objects. How can I do that?
    • David Ehrmann
      David Ehrmann almost 8 years
      Would url-encoding work, too? The resultant string might be smaller than if it's base 64-encoded.
  • johni
    johni almost 8 years
    Thanks 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
    xiaofeng.li almost 8 years
    How big is your JSON? I tried some large ones and it's working fine.
  • johni
    johni almost 8 years
    Yep, you're right. I probably selected only part of the encoded string.
  • Cinn
    Cinn over 6 years
    new Buffer() is now deprecated, you should use Buffer.from() instead, see the doc
  • Kugel
    Kugel almost 6 years
    Have you tried this with non ASCII characters? atob and btoa are quite broken.
  • Bender
    Bender over 4 years
    this won't work with not escaped charecters in JSON
  • roshnet
    roshnet almost 4 years
    @Bender can you please give an example?
  • Mo.
    Mo. over 2 years
    @Kugel Any other solution available ?