flatted
Drop-in JSON replacement that serializes and revives circular references and repeated object graphs without loss.
Official Documentation ↗v3. Check the official docs for the latest API surface.Installation
npm install flattedpnpm add flattedMethods
No methods match this filter.
Overview
flatted serializes and revives circular or shared object graphs as JSON text, replacing ancestor references with indices. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
stringify— Serializes the value to JSON text while encoding references so cycles survive round-trips.parse— Revives JSON text produced by flatted, restoring shared references and cycles.toJSON— Converts a flatted index structure into a plain value with cycles restored in memory.
Example
const { stringify } = require('flatted');
const node = { name: 'root' };
node.self = node;
const text = stringify(node);
Notes
The output is JSON text with a structure only flatted.parse decodes correctly; plain JSON.parse will not restore references.
Verification
Serialize a self-referencing object, parse it back, and confirm the inner reference equals the outer object.
API reference evidence
Fetched from official sources on 2026-11-08T17:50:40.000Z via WebFetch https://raw.githubusercontent.com/WebReflection/flatted/main/README.md.
stringify→ signature from https://raw.githubusercontent.com/WebReflection/flatted/main/README.md; official text: “Added a replacer and aspaceparameter to.stringify(object, replacer, space)for feature parity with JSON sig”parse→ signature from https://raw.githubusercontent.com/WebReflection/flatted/main/README.md; official text: “Added a reviver parameter to.parse(string, reviver)and revive your own objects.”
FAQ
- Why does JSON.stringify fail on circular data?
- JSON.stringify throws a TypeError because it cannot represent a reference back to an ancestor; flatted replaces those references with indices.
- Is the output valid JSON?
- It is valid JSON text with a special structure that flatted.parse understands; a plain JSON.parse will not restore the cycles.
- Does flatted handle shared non-circular references?
- Yes. Repeated references are encoded once and revived as the same object instance.
Report a problem with this API docOpens a pre-filled issue on GitHub.