69

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?

3
  • Why would you expect them equal? Jun 30, 2016 at 22:25
  • Well, I'm trying to do with base-64 what JSON.parse(JSON.stringify(obj)) does on objects. How can I do that?
    – johni
    Jun 30, 2016 at 22:26
  • Would url-encoding work, too? The resultant string might be smaller than if it's base 64-encoded. Jun 30, 2016 at 22:32

4 Answers 4

156

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

4
  • 11
    Have you tried this with non ASCII characters? atob and btoa are quite broken.
    – Kugel
    Jul 10, 2018 at 7:42
  • 3
    this won't work with not escaped charecters in JSON
    – Bender
    Oct 17, 2019 at 10:22
  • 1
    @Bender can you please give an example?
    – roshnet
    Jun 1, 2020 at 1:28
  • @Kugel Any other solution available ?
    – Mo.
    Jan 7, 2022 at 9:26
83

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");
4
  • 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.
    – johni
    Jun 30, 2016 at 22:42
  • How big is your JSON? I tried some large ones and it's working fine. Jun 30, 2016 at 22:54
  • Yep, you're right. I probably selected only part of the encoded string.
    – johni
    Jun 30, 2016 at 22:55
  • 3
    new Buffer() is now deprecated, you should use Buffer.from() instead, see the doc
    – Cinn
    Oct 13, 2017 at 14:14
14

You can easily encode and decode from and to JSON/Base64 using a Buffer:

JSON to Base64:

function jsonToBase64(object) {
  const json = JSON.stringify(object);
  return Buffer.from(json).toString("base64");
}

Base64 to JSON:

function base64ToJson(base64String) {
  const json = Buffer.from(base64String, "base64").toString();
  return JSON.parse(json);
}

atob() and btoa() are outdated and should no longer be used.

2

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())));

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.