Node.js – Convert JSON to Buffer

To convert JSON to Buffer in Node.js, convert the JavaScript object to a JSON string with JSON.stringify(), and then pass that string to Buffer.from(). A Buffer stores binary data, so the JSON object must first become a string before Node.js can place its bytes in a Buffer.

The usual pattern is shown below.

</>
Copy
const jsonString = JSON.stringify(jsonObject);
const buffer = Buffer.from(jsonString, 'utf8');

In this tutorial, we will convert JSON to Buffer using JSON.stringify() and Buffer.from(), check the size of the Buffer, and convert the Buffer data back to a JavaScript object for verification.

Why JSON must be stringified before creating a Node.js Buffer

A JSON-like value in a Node.js program is usually a JavaScript object. Buffer.from() cannot directly store object structure such as keys, values, arrays, or nested objects. It stores bytes. Therefore, the object should be serialized to a JSON string first.

The conversion has two clear steps:

  • JSON.stringify(jsonObj) converts the JavaScript object to a JSON string.
  • Buffer.from(jsonStr, 'utf8') creates a Buffer from the UTF-8 bytes of that string.

Using 'utf8' explicitly is optional because UTF-8 is the default encoding for strings in Buffer.from(), but writing it makes the conversion easy to read.

Example 1 – JSON to Buffer

In this example, we will take a JSON string, convert it to JSON Object, and then convert this JSON Object to a Buffer.

convert-json-to-buffer.js

</>
Copy
const msg = '{"name":"John", "age":"22"}';
var jsonObj = JSON.parse(msg);

// convert JSON object to String
var jsonStr = JSON.stringify(jsonObj);

// read json string to Buffer
const buf = Buffer.from(jsonStr);

console.log(buf.length);

Output

$ node convert-json-to-buffer.js 
26

The output is the number of bytes stored in the Buffer. For this JSON string, the Buffer length is 26.

Convert a JavaScript object directly to a Buffer

If you already have a JavaScript object, you do not need to call JSON.parse(). You can stringify the object and create the Buffer directly.

object-to-buffer.js

</>
Copy
const user = {
  name: 'John',
  age: 22,
  active: true
};

const jsonString = JSON.stringify(user);
const buffer = Buffer.from(jsonString, 'utf8');

console.log(buffer);

Output

<Buffer 7b 22 6e 61 6d 65 22 3a 22 4a 6f 68 6e 22 2c 22 61 67 65 22 3a 32 32 2c 22 61 63 74 69 76 65 22 3a 74 72 75 65 7d>

The Buffer display shows hexadecimal byte values. It does not mean the JSON data is lost. It means the string has been stored as bytes.

Convert Buffer back to JSON object in Node.js

After creating a Buffer from JSON, you can read it back by reversing the same process. First convert the Buffer to a string with toString('utf8'), and then parse the string with JSON.parse().

buffer-to-json-object.js

</>
Copy
const product = {
  id: 101,
  name: 'Keyboard',
  inStock: true
};

const buffer = Buffer.from(JSON.stringify(product), 'utf8');

const jsonString = buffer.toString('utf8');
const jsonObject = JSON.parse(jsonString);

console.log(jsonObject.name);
console.log(jsonObject.inStock);

Output

Keyboard
true

Using JSON to Buffer conversion for nested objects and arrays

The same method works for nested objects and arrays, as long as the value can be represented by JSON.stringify().

nested-json-buffer.js

</>
Copy
const order = {
  orderId: 5001,
  customer: {
    name: 'John',
    city: 'London'
  },
  items: ['Book', 'Pen', 'Notebook']
};

const buffer = Buffer.from(JSON.stringify(order), 'utf8');
const restoredOrder = JSON.parse(buffer.toString('utf8'));

console.log(restoredOrder.customer.city);
console.log(restoredOrder.items[1]);

Output

London
Pen

Important notes when converting JSON data to Buffer

  • Buffer.from() should receive the JSON string, not the original object.
  • Use JSON.parse() only when you start with a JSON string and need a JavaScript object.
  • Use JSON.stringify() before creating the Buffer.
  • Use buffer.toString('utf8') before parsing Buffer data back to JSON.
  • Values that are not valid JSON, such as functions and undefined properties, are not preserved by JSON.stringify().

Common mistake: passing an object directly to Buffer.from()

Do not pass a plain object directly to Buffer.from(). Convert it to a string first.

</>
Copy
// Correct
const buffer = Buffer.from(JSON.stringify(jsonObject), 'utf8');

If you need to learn more about Buffer methods, refer to the official Node.js Buffer documentation.

FAQs on converting JSON to Buffer in Node.js

How do I convert JSON to Buffer in Node.js?

Use JSON.stringify() to convert the object to a JSON string, and then use Buffer.from(jsonString, 'utf8') to create the Buffer.

Can Buffer.from() convert a JSON object directly?

No. A plain JavaScript object should be converted to a string first. Use Buffer.from(JSON.stringify(obj)) instead of passing the object directly.

How do I convert a Buffer back to a JSON object?

Convert the Buffer to a string using buffer.toString('utf8'), and then parse that string using JSON.parse().

Does JSON to Buffer conversion preserve nested objects?

Yes, nested objects and arrays are preserved when they are valid JSON values. They can be restored with JSON.parse(buffer.toString('utf8')).

What encoding should I use for JSON Buffer conversion?

UTF-8 is the usual encoding for JSON text. In Node.js, Buffer.from(jsonString) uses UTF-8 by default, but Buffer.from(jsonString, 'utf8') is clearer.

Node.js JSON to Buffer tutorial review checklist

  • The JSON object is serialized with JSON.stringify() before Buffer creation.
  • The Buffer is created with Buffer.from(jsonString, 'utf8') or the default UTF-8 encoding.
  • Buffer-to-object examples use buffer.toString('utf8') before JSON.parse().
  • Output blocks show only the result, not new command syntax.
  • Examples avoid unsupported values such as functions inside JSON data.

Summary of JSON to Buffer conversion in Node.js

In this Node.js Tutorial, we have learnt to convert JSON String to Buffer. The key step is to use JSON.stringify() before Buffer.from(). To read the data back, convert the Buffer to a UTF-8 string and parse it with JSON.parse().