What is json-stringify-deterministic?
The json-stringify-deterministic package is a Node.js library that provides a deterministic version of JSON.stringify. It ensures that the JSON string output is consistent across different runs by sorting the object keys, which is particularly useful for caching, hashing, or comparing JSON data.
What are json-stringify-deterministic's main functionalities?
Deterministic JSON Stringification
This feature allows you to stringify a JavaScript object into a JSON string with keys sorted in a deterministic order. This ensures that the same object will always produce the same JSON string, which is useful for consistent hashing or comparison.
const stringify = require('json-stringify-deterministic');
const obj = { b: 1, a: 2 };
const jsonString = stringify(obj);
console.log(jsonString);
Other packages similar to json-stringify-deterministic
fast-json-stable-stringify
fast-json-stable-stringify is another package that provides deterministic JSON.stringify functionality by sorting object keys. It is known for its performance and is often used in environments where speed is critical. Compared to json-stringify-deterministic, it offers similar functionality but may have different performance characteristics.
json-stable-stringify
json-stable-stringify is a widely used package that also provides deterministic JSON stringification by sorting object keys. It is one of the older packages in this space and is known for its stability and reliability. Compared to json-stringify-deterministic, it serves a similar purpose but may have different API options and performance.
json-stringify-deterministic

Deterministic version of JSON.stringify()
, so you can get a consistent hash from stringified results.
Similar to json-stable-stringify but:
- No Dependencies. Minimal as possible.
- Better cycles detection.
- Support serialization for object without
.toJSON
(such as RegExp
).
- Provides built-in TypeScript declarations.
Install
npm install json-stringify-deterministic --save
Usage
const stringify = require('json-stringify-deterministic')
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }
console.log(stringify(obj))
API
stringify(<obj>, [opts])
obj
Required
Type: object
The input object
to be serialized.
opts
opts.stringify
Type: function
Default: JSON.stringify
Determinate how to stringify primitives values.
opts.cycles
Type: boolean
Default: false
Determinate how to resolve cycles.
Under true
, when a cycle is detected, [Circular]
will be inserted in the node.
opts.compare
Type: function
Custom comparison function for object keys.
Your function opts.compare
is called with these parameters:
opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })
For example, to sort on the object key names in reverse order you could write:
const stringify = require('json-stringify-deterministic')
const obj = { c: 8, b: [{z: 6,y: 5,x: 4}, 7], a: 3 }
const objSerializer = stringify(obj, function (a, b) {
return a.key < b.key ? 1 : -1
})
console.log(objSerializer)
Or if you wanted to sort on the object values in reverse order, you could write:
const stringify = require('json-stringify-deterministic')
const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 }
const objtSerializer = stringify(obj, function (a, b) {
return a.value < b.value ? 1 : -1
})
console.log(objtSerializer)
opts.space
Type: string
Default: ''
If you specify opts.space
, it will indent the output for pretty-printing.
Valid values are strings (e.g. {space: \t}
). For example:
const stringify = require('json-stringify-deterministic')
const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } }
const objSerializer = stringify(obj, { space: ' ' })
console.log(objSerializer)
opts.replacer
Type: function
The replacer parameter is a function opts.replacer(key, value)
that behaves
the same as the replacer
from the core JSON object.
Related
License
MIT © Kiko Beats.