Which is Better JSON Stringification Libraries?
flatted vs json-stringify-safe vs json-stable-stringify vs fast-json-stringify vs circular-json vs fast-stable-stringify
1 Year
flattedjson-stringify-safejson-stable-stringifyfast-json-stringifycircular-jsonfast-stable-stringifySimilar Packages:
What's JSON Stringification Libraries?

These libraries provide various functionalities for serializing JavaScript objects into JSON format, each with unique features tailored for specific use cases. They address common issues such as circular references, performance optimization, and maintaining the order of object keys, making them essential tools for developers working with JSON data in JavaScript applications.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
flatted37,723,9791,03140.3 kB27 months agoISC
json-stringify-safe22,310,081550-79 years agoISC
json-stable-stringify6,119,3785227.7 kB79 months agoMIT
fast-json-stringify2,123,8223,487368 kB303 months agoMIT
circular-json1,192,966604-06 years agoMIT
fast-stable-stringify275,08629-07 years agoMIT
Feature Comparison: flatted vs json-stringify-safe vs json-stable-stringify vs fast-json-stringify vs circular-json vs fast-stable-stringify

Circular Reference Handling

  • flatted: Flatted supports circular references, enabling you to serialize complex objects without issues, making it a versatile choice.
  • json-stringify-safe: JSON Stringify Safe can handle circular references by replacing them with a placeholder, allowing for safe serialization.
  • json-stable-stringify: JSON Stable Stringify does not handle circular references and will throw an error if they are present.
  • fast-json-stringify: Fast JSON Stringify does not support circular references and will throw an error if encountered, making it unsuitable for such cases.
  • circular-json: Circular JSON can handle circular references seamlessly, allowing you to stringify objects that reference themselves without throwing errors.
  • fast-stable-stringify: Fast Stable Stringify also does not support circular references, focusing instead on performance and key ordering.

Performance

  • flatted: Flatted offers a good balance of performance and functionality, though it may not match the speed of fast-json-stringify for large datasets.
  • json-stringify-safe: JSON Stringify Safe is not specifically optimized for performance and may be slower than other libraries when handling large datasets.
  • json-stable-stringify: JSON Stable Stringify is slower than some alternatives due to its focus on maintaining key order, which can add overhead during serialization.
  • fast-json-stringify: Fast JSON Stringify is highly optimized for performance, making it one of the fastest options available for JSON serialization, especially with large objects.
  • circular-json: Circular JSON is not optimized for speed and may be slower than other libraries when handling large datasets due to its focus on circular reference support.
  • fast-stable-stringify: Fast Stable Stringify is also performance-oriented, providing fast serialization while maintaining key order, though it may not be as fast as fast-json-stringify.

Key Order Consistency

  • flatted: Flatted does not guarantee key order, prioritizing circular reference handling over key consistency.
  • json-stringify-safe: JSON Stringify Safe does not maintain key order and focuses on safe serialization instead.
  • json-stable-stringify: JSON Stable Stringify ensures that the keys are always output in a consistent order, making it suitable for testing and caching scenarios.
  • fast-json-stringify: Fast JSON Stringify does not maintain key order, focusing instead on speed, which may be a drawback in scenarios where order matters.
  • circular-json: Circular JSON does not guarantee key order in the output, which may lead to inconsistent results when serializing objects with unordered keys.
  • fast-stable-stringify: Fast Stable Stringify guarantees consistent key ordering in the output, making it ideal for applications requiring deterministic JSON results.

Ease of Use

  • flatted: Flatted offers a modern API that is easy to use, making it a good choice for developers familiar with JSON serialization.
  • json-stringify-safe: JSON Stringify Safe provides a simple API for safe serialization, making it easy to integrate into projects where circular references are a concern.
  • json-stable-stringify: JSON Stable Stringify is straightforward to use, but the focus on key order may require developers to consider their data structures more carefully.
  • fast-json-stringify: Fast JSON Stringify has a straightforward API, but may require additional setup for schema definitions to achieve optimal performance.
  • circular-json: Circular JSON is easy to use with a simple API, making it accessible for developers needing to handle circular references without complex configurations.
  • fast-stable-stringify: Fast Stable Stringify is user-friendly, with a simple API that allows for quick implementation while ensuring key order consistency.

Use Cases

  • flatted: Flatted is versatile and can be used in various applications, especially those that require handling complex objects with circular references.
  • json-stringify-safe: JSON Stringify Safe is suitable for applications that need to serialize objects with potential circular references without crashing.
  • json-stable-stringify: JSON Stable Stringify is useful in scenarios where predictable JSON output is necessary, such as versioning or comparing JSON data.
  • fast-json-stringify: Fast JSON Stringify is ideal for high-performance applications, such as APIs or microservices, where speed is critical and data structures are well-defined.
  • circular-json: Circular JSON is best suited for debugging and logging scenarios where complex objects with circular references need to be serialized without errors.
  • fast-stable-stringify: Fast Stable Stringify is perfect for applications that require consistent JSON outputs, such as testing frameworks or caching mechanisms.
How to Choose: flatted vs json-stringify-safe vs json-stable-stringify vs fast-json-stringify vs circular-json vs fast-stable-stringify
  • flatted: Use Flatted for a balance of performance and the ability to handle circular references. It is a modern alternative that supports both serialization and deserialization of complex objects, making it versatile for various applications.
  • json-stringify-safe: Select JSON Stringify Safe if you want to avoid errors from circular references while still maintaining a straightforward JSON serialization process. It provides a simple API for safe stringification without additional features.
  • json-stable-stringify: Choose JSON Stable Stringify when you need to ensure that the output is always in a consistent order. This is useful for generating predictable JSON outputs, especially in scenarios where the output is compared or stored.
  • fast-json-stringify: Opt for Fast JSON Stringify when performance is a priority. It is designed for speed and can significantly reduce serialization time, making it suitable for high-performance applications where JSON serialization is a bottleneck.
  • circular-json: Choose Circular JSON if you need to serialize objects that may contain circular references. It allows you to safely stringify such objects without throwing errors, making it ideal for debugging or logging complex data structures.
  • fast-stable-stringify: Select Fast Stable Stringify if you require consistent key ordering in your JSON output. This is particularly useful when the order of keys matters, such as when generating deterministic outputs for testing or caching.
README for flatted

flatted

Downloads Coverage Status Build Status License: ISC WebReflection status

snow flake

Social Media Photo by Matt Seymour on Unsplash

A super light (0.5K) and fast circular JSON parser, directly from the creator of CircularJSON.

Available also for PHP.

Available also for Python.


Announcement 📣

There is a standard approach to recursion and more data-types than what JSON allows, and it's part of the Structured Clone polyfill.

Beside acting as a polyfill, its @ungap/structured-clone/json export provides both stringify and parse, and it's been tested for being faster than flatted, but its produced output is also smaller than flatted in general.

The @ungap/structured-clone module is, in short, a drop in replacement for flatted, but it's not compatible with flatted specialized syntax.

However, if recursion, as well as more data-types, are what you are after, or interesting for your projects/use cases, consider switching to this new module whenever you can 👍


npm i flatted

Usable via CDN or as regular module.

// ESM
import {parse, stringify, toJSON, fromJSON} from 'flatted';

// CJS
const {parse, stringify, toJSON, fromJSON} = require('flatted');

const a = [{}];
a[0].a = a;
a.push(a);

stringify(a); // [["1","0"],{"a":"0"}]

toJSON and fromJSON

If you'd like to implicitly survive JSON serialization, these two helpers helps:

import {toJSON, fromJSON} from 'flatted';

class RecursiveMap extends Map {
  static fromJSON(any) {
    return new this(fromJSON(any));
  }
  toJSON() {
    return toJSON([...this.entries()]);
  }
}

const recursive = new RecursiveMap;
const same = {};
same.same = same;
recursive.set('same', same);

const asString = JSON.stringify(recursive);
const asMap = RecursiveMap.fromJSON(JSON.parse(asString));
asMap.get('same') === asMap.get('same').same;
// true

Flatted VS JSON

As it is for every other specialized format capable of serializing and deserializing circular data, you should never JSON.parse(Flatted.stringify(data)), and you should never Flatted.parse(JSON.stringify(data)).

The only way this could work is to Flatted.parse(Flatted.stringify(data)), as it is also for CircularJSON or any other, otherwise there's no granted data integrity.

Also please note this project serializes and deserializes only data compatible with JSON, so that sockets, or anything else with internal classes different from those allowed by JSON standard, won't be serialized and unserialized as expected.

New in V1: Exact same JSON API

  • Added a reviver parameter to .parse(string, reviver) and revive your own objects.
  • Added a replacer and a space parameter to .stringify(object, replacer, space) for feature parity with JSON signature.

Compatibility

All ECMAScript engines compatible with Map, Set, Object.keys, and Array.prototype.reduce will work, even if polyfilled.

How does it work ?

While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. *

Once parsed, all indexes will be replaced through the flattened collection.

* represented as string to avoid conflicts with numbers

// logic example
var a = [{one: 1}, {two: '2'}];
a[0].a = a;
// a is the main object, will be at index '0'
// {one: 1} is the second object, index '1'
// {two: '2'} the third, in '2', and it has a string
// which will be found at index '3'

Flatted.stringify(a);
// [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]
// a[one,two]    {one: 1, a}    {two: '2'}  '2'