base64 JSON encoded strings in nodejs base64 JSON encoded strings in nodejs json json

base64 JSON encoded strings in nodejs


var buff = new Buffer(JSON.stringify({"hello":"world"})).toString("base64");


To complete @ladenedge's comment for clarity reasons:

var buff = Buffer.from(JSON.stringify({"hello":"world"})).toString("base64")


You can always prettify above code by providing some spacing, so that when some one decode it back to JSON String it would look good.

var buff = Buffer.from(JSON.stringify({"hello":"world"},undefined,n)).toString("base64")

n = 1 to 10 (Spacing)