The Buffer.toJSON()
method returns the buffer object as
JSON
:
key/value
pairs.{
"name" : "John",
"age" : 30
}
name
and age
are keys and John
and 30
are values, respectively.Key:Value
pairs are separated by a comma ,
.buffer.toJSON()
This method does not take any parameters.
The toJSON()
method returns the JSON
format of the buffer object
on which it is called. Check out the following examples to understand how it works.
In the following example:
buf
from the fill as abcd
.4
we convert the buffer object buf
into JSON
format with the buf.toJSON()
method.type
and data
. Here type
is Buffer
and data
is the data present in the buffer object buf
.const buf = Buffer.from('abcd')//Note: tojson() or toJson() will throw error, mention exact case.console.log(buf.toJSON())
The following code snippet is the same as Example 1, except that we pass base64
as encoding format to encode the fill abcd
.
const buf = Buffer.from('abcd','base64')console.log(buf.toJSON())