xxxxxxxxxx
var buf = Buffer.from(JSON.stringify(obj));
xxxxxxxxxx
var obj = {
key:'value',
key:'value',
key:'value',
key:'value',
key:'value'
}
var buf = new Buffer.from(obj.toString());
console.log('Real Buffer ' + buf); //This prints --> Real Buffer <Buffer 5b 6f 62 6a 65 63 74>
var temp = buf.toString();
console.log('Buffer to String ' + buf); //This prints --> Buffer to String [object Object]
xxxxxxxxxx
const jsonObject = {
"Name":'Ram',
"Age":'28',
"Dept":'IT'
}
const encodedJsonObject = Buffer.from(JSON.stringify(jsonObject)).toString('base64');
console.log('--encodedJsonObject-->', encodedJsonObject)
//Output --encodedJsonObject--> eyJOYW1lIjoiUmFtIiwiQWdlIjoiMjgiLCJEZXB0IjoiSVQifQ==
const decodedJsonObject = Buffer.from(encodedJsonObject, 'base64').toString('ascii');
console.log('--decodedJsonObject-->', JSON.parse(decodedJsonObject))
//Output --decodedJsonObject--> {Name: 'Ram', Age: '28', Dept: 'IT'}