Convert a JSON Object to Buffer and Buffer to JSON Object back

You need to stringify the json, not calling toString

var buf = Buffer.from(JSON.stringify(obj));

And for converting string to json obj :

var temp = JSON.parse(buf.toString());

as @Ebrahim you have to use first JSON.stringify to convert the JSON to string, and then you can convert it to a Buffer. But it's little bit risky to use JSON.stringify since it can break your app in case of self refrencing or circular referncing:

   var y={}
   y.a=y
   JSON.stringify(y) // Uncaught TypeError: Converting circular structure to JSON

If you have such a risk, it better to use a safe stringify solution like this the npm module safe-json-stringify

And then you have to do:

// convert to buffer:
const stringify = require('safe-json-stringify');
var buf = Buffer.from(stringify(obj));

// convert from buffer:
var temp = JSON.parse(buf.toString());


enter image description here

Kindly copy below snippet

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'}