182

I have a JSON object and I'm converting it to a Buffer and doing some process here. Later I want to convert the same buffer data to convert to valid JSON object.

I'm working on Node V6.9.1

Below is the code I tried but I'm getting [object object] when I convert back to JSON and cannot open this object.

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]

So I tried to print whole object using inspect way

console.log('Full temp ' + require('util').inspect(buf, { depth: null }));  //This prints --> '[object object]' [not printing the obj like declared above]

If i try to read it like an array

 console.log(buf[0]);  // This prints --> [ 

I tried parsing also it throw SyntaxError: Unexpected token o in JSON at position 2

I need to view it as real object like I created (I mean like declared above).

Please help..

2

3 Answers 3

346

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());
6
  • 3
    This will not work if is there's another buffer field in obj
    – gilamran
    Oct 14, 2018 at 11:44
  • 17
    actually, toString is not needed here. groups.google.com/forum/#!topic/nodejs/hybuh7DbQkM
    – Dzenly
    Nov 30, 2018 at 17:17
  • 1
    for me, it worked when I took away the JSON.stringify and put the object directly inside the parameters, like so: var buf = Buffer.from({ key:'value', key:'value', key:'value', key:'value', key:'value' }); Mar 18, 2020 at 21:56
  • 1
    @JorgeMauricio Buffer.from() does not work with objects or (technically) arrays. That should be an error, but I think your object is getting converted to a string. In short, just use Buffer.from(JSON.stringify(...)) and JSON.parse(buffer.toString()) for simple objects. If, however, you have buffers stored in the object, you''l have to write a recursive function to walk to the bottom of the object, and convert all buffers to strings before you stringify the object into a buffer, then do the inverse on the other side
    – Werlious
    Sep 2, 2020 at 22:14
  • 2
    @Dzenly while toString is technically not needed, it should be used. Reading the page you linked, it states that the buffer is converted to a string (using Buffer.toString) using utf8 encoding, and while normally this is not an issue, if the buffer was created in some other encoding, you will get garbage. So really, for things like this, it should be fine, but for a lot of other scenarios (where you have little to no control over things passed in), knowing and respecting the encoding is a must
    – Werlious
    Sep 2, 2020 at 22:20
7

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

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

Your Answer

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.