# toJSON > Return a [JSON][json] representation of a [Buffer][@stdlib/buffer/ctor].
## Usage ```javascript var toJSON = require( '@stdlib/buffer/to-json' ); ``` #### toJSON( buffer ) Returns a [JSON][json] representation of a [`Buffer`][@stdlib/buffer/ctor]. ```javascript var array2buffer = require( '@stdlib/buffer/from-array' ); var buf = array2buffer( [ 1, 2 ] ); var json = toJSON( buf ); /* returns { 'type': 'Buffer', 'data': [ 1, 2 ] } */ ``` For guidance on reviving a JSON-serialized [`Buffer`][@stdlib/buffer/ctor], see [`reviver()`][@stdlib/buffer/reviver].
## Examples ```javascript var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' ); var randint = require( '@stdlib/random/base/discrete-uniform' ); var toJSON = require( '@stdlib/buffer/to-json' ); var buf; var i; buf = allocUnsafe( 100 ); for ( i = 0; i < buf.length; i++ ) { buf[ i ] = randint( 0, 255 ); } console.log( toJSON( buf ) ); ```