Which is Better JSON Stringification Libraries?
json-stable-stringify-without-jsonify vs json-stable-stringify vs json-stringify-deterministic
1 Year
json-stable-stringify-without-jsonifyjson-stable-stringifyjson-stringify-deterministic
What's JSON Stringification Libraries?

JSON stringification libraries are designed to convert JavaScript objects into JSON strings while ensuring a consistent order of keys. This is particularly important in scenarios where the order of keys can affect the outcome, such as when generating hashes or comparing JSON objects. These libraries provide various features to enhance the reliability and predictability of JSON serialization, making them essential tools for developers working with APIs, data storage, or any application where JSON is a primary data format.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
json-stable-stringify-without-jsonify38,321,95010-18 years agoMIT
json-stable-stringify7,669,2415627.7 kB710 months agoMIT
json-stringify-deterministic88,5203611.6 kB0a year agoMIT
Feature Comparison: json-stable-stringify-without-jsonify vs json-stable-stringify vs json-stringify-deterministic

Key Ordering

  • json-stable-stringify-without-jsonify: Similar to its counterpart, this library also sorts keys consistently but does so without relying on the built-in JSON.stringify method, making it a more lightweight option for performance-sensitive applications.
  • json-stable-stringify: This package ensures that the keys of objects are sorted in a consistent order before stringification, which is crucial for generating predictable JSON outputs that can be reliably compared or hashed.
  • json-stringify-deterministic: This library guarantees that the output is deterministic, meaning that for the same input, the output will always be the same, even for complex nested structures.

Performance

  • json-stable-stringify-without-jsonify: This package is optimized for performance and is designed to be faster than json-stable-stringify, making it suitable for high-performance applications where speed is a priority.
  • json-stable-stringify: While this package is reliable, it may not be the fastest option available due to the overhead of sorting keys. It is best used when consistency is more critical than speed.
  • json-stringify-deterministic: This library may introduce some performance overhead due to its handling of complex objects, but it provides a balance between performance and the need for deterministic output.

Complex Object Handling

  • json-stable-stringify-without-jsonify: Does not inherently manage circular references, so developers need to ensure that the objects being stringified do not contain such structures.
  • json-stable-stringify: Handles standard JavaScript objects well but may struggle with circular references unless additional handling is implemented.
  • json-stringify-deterministic: This library is designed to handle complex objects, including those with circular references, making it a robust choice for applications that require deep object serialization.

Dependencies

  • json-stable-stringify-without-jsonify: As the name suggests, this package does not depend on JSON.stringify, which can be advantageous in environments where minimizing dependencies is critical.
  • json-stable-stringify: This package is lightweight and has minimal dependencies, making it easy to integrate into existing projects without adding significant overhead.
  • json-stringify-deterministic: This library may have additional dependencies due to its advanced features, so developers should consider the trade-off between functionality and the size of their dependency tree.

Use Cases

  • json-stable-stringify-without-jsonify: Best suited for performance-critical applications where the overhead of JSON.stringify is a concern, such as real-time data processing.
  • json-stable-stringify: Ideal for applications that require consistent JSON outputs for hashing or comparison, such as API responses or configuration files.
  • json-stringify-deterministic: Perfect for applications that need to serialize complex data structures reliably, such as state management in applications or deep comparisons.
How to Choose: json-stable-stringify-without-jsonify vs json-stable-stringify vs json-stringify-deterministic
  • json-stable-stringify-without-jsonify: Select json-stable-stringify-without-jsonify if you want a lightweight alternative that does not depend on the JSON.stringify method. This package is ideal for environments where performance is critical and you want to avoid the overhead of additional function calls.
  • json-stable-stringify: Choose json-stable-stringify if you need a reliable solution for stringifying JSON objects while ensuring that the keys are sorted in a stable manner. It is particularly useful when you want to generate consistent output for the same input across different runs.
  • json-stringify-deterministic: Opt for json-stringify-deterministic if you require a library that guarantees deterministic output for complex objects, including those with circular references. This package is well-suited for scenarios where you need to ensure that the stringified output is consistent and can be reliably compared.
README for json-stable-stringify-without-jsonify

json-stable-stringify

This is the same as https://github.com/substack/json-stable-stringify but it doesn't depend on libraries without licenses (jsonify).

deterministic version of JSON.stringify() so you can get a consistent hash from stringified results

You can also pass in a custom comparison function.

browser support

build status

example

var stringify = require('json-stable-stringify');
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
console.log(stringify(obj));

output:

{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}

methods

var stringify = require('json-stable-stringify')

var str = stringify(obj, opts)

Return a deterministic stringified string str from the object obj.

options

cmp

If opts is given, you can supply an opts.cmp to have a custom comparison function for object keys. Your function opts.cmp 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:

var stringify = require('json-stable-stringify');

var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
var s = stringify(obj, function (a, b) {
    return a.key < b.key ? 1 : -1;
});
console.log(s);

which results in the output string:

{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}

Or if you wanted to sort on the object values in reverse order, you could write:

var stringify = require('json-stable-stringify');

var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
var s = stringify(obj, function (a, b) {
    return a.value < b.value ? 1 : -1;
});
console.log(s);

which outputs:

{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}

space

If you specify opts.space, it will indent the output for pretty-printing. Valid values are strings (e.g. {space: \t}) or a number of spaces ({space: 3}).

For example:

var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
var s = stringify(obj, { space: '  ' });
console.log(s);

which outputs:

{
  "a": {
    "and": [
      1,
      2,
      3
    ],
    "foo": "bar"
  },
  "b": 1
}

replacer

The replacer parameter is a function opts.replacer(key, value) that behaves the same as the replacer from the core JSON object.

install

With npm do:

npm install json-stable-stringify

license

MIT