Skip to main content
Buffer.toJSON - node__buffer.d.ts - Node documentation
method Buffer.toJSON

Usage in Deno

```typescript import { type Buffer } from "node:node__buffer.d.ts"; ```
Buffer.toJSON(): { type: "Buffer"; data: number[]; }
Returns a JSON representation of `buf`. [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) implicitly calls this function when stringifying a `Buffer` instance. `Buffer.from()` accepts objects in the format returned from this method. In particular, `Buffer.from(buf.toJSON())` works like `Buffer.from(buf)`. ```js import { Buffer } from 'node:buffer'; const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5]); const json = JSON.stringify(buf); console.log(json); // Prints: {"type":"Buffer","data":[1,2,3,4,5]} const copy = JSON.parse(json, (key, value) => { return value && value.type === 'Buffer' ? Buffer.from(value) : value; }); console.log(copy); // Prints: ```

Return Type

{ type: "Buffer"; data: number[]; }