Convert between Strings, Buffers and Objects in Node.js

Much of what you’ll read here is available on the official docs and with a bit of time you could’ve collected the information yourself. But since I’ve already did that part and want to have a plain “summary” of it, I’d say a good blog post about it won’t hurt. If you already know what’s described here, perhaps this will serve as a quick reference/cheatsheet.

Preliminary notes

When speaking about converting strings to objects and vice versa, the string must hold data in a well defined format. JSON is a format that has native language support in both browser and Node (it means Javascript Object Notation, after all) therefore I’ll go with it for this post. For the sake of a zoomed out perspective, I’ll insert equivalent snippets for the YAML format (npm link).

When talking about objects, I mean the plain old JavaScript object, having string keys and values of any depth of objects, strings, arrays, numbers, boolean. This means we are excluding other types such as Date, Map or Set.

Buffers are sequences of values representing bytes therefore converting strings strictly into/from buffers can be done using arbitrary values. I’m explicitly specifying the already default utf-8 encoding to hint that it can be changed.

Converting Javascript objects to Buffers and the reverse operation will have you do an intermediary string conversion ,usually. However you can use binary serialization formats, therefore I’ve included a BSON example (npm link).

Quick links to jump to:

Convert JSON String to Object

1
2
let s = `{ "id":1, "name": "Jean" }`
let o = JSON.parse(s);

Convert Object to JSON String

1
2
3
4
5
let o = {
id: 1,
name: "Jean",
};
let s = JSON.stringify(o);

Convert YAML String to Object

1
2
3
4
5
6
7
const YAML = require('yaml');

let y = `
id: 1
name: Jean
`;
let o = YAML.parse(y);

Convert Object to YAML string

1
2
3
4
5
6
7
const YAML = require('yaml');

let o = {
id: 1,
name: "Jean"
};
let s = YAML.stringify(o);

Convert String to Buffer

1
2
let s = 'arbitrary';
let b = Buffer.from(s, 'utf-8');

Convert Buffer to String

1
2
3
let b = Buffer.from([0x61, 0x72, 0x62, 0x69, 0x74, 0x72, 0x61, 0x72, 0x79]);
let s = b.toString('utf-8');
// "arbitrary"

Convert Object to Buffer

1
2
3
let o = { id: 1 };
let s = JSON.stringify(o);
let b = Buffer.from(s, 'utf-8');

Convert Buffer to Object

1
2
3
4
let b = Buffer.from([0x7b, 0x22, 0x69, 0x64, 0x22, 0x3a, 0x31, 0x7d]);
let s = b.toString('utf-8');
let o = JSON.parse(s); // passing the buffer directly will have it converted to string
// {id: 1 }

Convert Object to Buffer directly

1
2
3
4
const BSON = require('bson');

let o = { id: 1 };
let b = BSON.serialize(o);

Covert Buffer to Object directly

1
2
3
4
5
const BSON = require('bson');

let b = Buffer.from([0x0d, 0x00, 0x00, 0x00, 0x10, 0x69, 0x64, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]);
let o = BSON.deserialize(b);
// { id: 1 }

Closing Notes

The above attempts to be a simple gist for such transformations in Node.js. I hope it saves you some time.

Stay tuned (I know, tall order) because I’m thinking of writing about how/when/why/if streams come into play.

Thumbnail image copyright © 1968 Karel Thole.