What is flatted?
The flatted npm package is a library for encoding and decoding JavaScript objects into a flat JSON format. It is particularly useful for handling circular references and complex data structures that JSON.stringify cannot handle by default.
What are flatted's main functionalities?
Encoding objects with circular references
Flatted allows you to encode objects that have circular references, which would throw an error with JSON.stringify. For example, if you have an object 'circularObj' that references itself, you can use flatted.stringify(circularObj) to get a string representation of the object.
{"encoded": "flatted.stringify(circularObj)"}
Decoding flatted strings back to objects
After encoding an object with circular references using flatted, you can decode the resulting string back into an object with flatted.parse(flattedString). This will restore the circular references and give you back the original object structure.
{"decoded": "flatted.parse(flattedString)"}
Other packages similar to flatted
circular-json
CircularJSON is a JSON-like format that can encode circular structures and other complex data types. It is similar to flatted but was superseded by flatted due to its ability to handle more data types and its smaller footprint.
json-stringify-safe
json-stringify-safe is a package that can stringify objects with circular references without throwing. It works by replacing circular references with a string '[Circular]'. It is similar to flatted but does not allow for reconstruction of the original object with circular references.
flatted
A super light (0.5K) and fast circular JSON parser, directly from the creator of CircularJSON.
npm i flatted
Usable via CDN or as regular module.
import {parse, stringify} from 'flatted';
const {parse, stringify} = require('flatted');
const a = [{}];
a[0].a = a;
a.push(a);
stringify(a);
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
var a = [{one: 1}, {two: '2'}];
a[0].a = a;
Flatted.stringify(a);