Flatted is a lightweight (0.5KB) and high-performance JavaScript library designed for serializing and deserializing JavaScript objects that contain circular references, a common limitation of standard `JSON.stringify` and `JSON.parse`. It achieves this by flattening circular structures and replacing references with unique string indices. The library maintains an API surface that mirrors the native `JSON` object, including support for `reviver` and `replacer` functions since v1, making it familiar for developers. Currently at version 3.4.2, `flatted` is actively maintained by the creator of CircularJSON, offering a stable and mature solution for handling complex object graphs. It differentiates itself by its minimal footprint and focus on speed for JSON-compatible values, providing a targeted alternative to more comprehensive serialization libraries like `structured-clone` for specific use cases.
npm install flattedVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `stringify` and `parse` to handle circular references in objects and arrays, preserving their original structure upon deserialization.
Always use `Flatted.stringify` with `Flatted.parse` (e.g., `Flatted.parse(Flatted.stringify(data))`).
For richer data types or non-JSON compatible objects, consider using a structured clone polyfill or other libraries that explicitly handle these types, such as `@ungap/structured-clone`, or implement custom `toJSON`/`fromJSON` methods.
Ensure you are using `flatted` version 1.0.0 or later for full API compatibility with native JSON signatures (replacer, reviver, space arguments).
Replace `JSON.stringify(obj)` with `stringify(obj)` from 'flatted'.
Replace `JSON.parse(str)` with `parse(str)` from 'flatted'.
Use ESM import syntax: `import { parse, stringify } from 'flatted';`.No dependency data recorded yet.