A way to truncate a json object. Useful for circular referenced objects.
npm install json-truncate --save
// You can add this as a static function on JSON.
JSON.truncate = require('json-truncate');
console.log(JSON.truncate(SomeDeepObject, 10));
//OR specify a replacement string for truncated values
console.log(JSON.truncate(SomeDeepObject, 10, {replace: '[Truncated]'}));
You will get a proper truncated object that can now be written to a file if needed.
obj
- The Object that will be truncated.maxDepth
- (optional) The depth at which to stop building the valid json. Defaults to10
.options
- (optional) An option object to customize the behavior of the utility. Defaults to{}
.
Current Option Properties
Option | Description |
---|---|
replace | A string value that is used to replace all truncated values. If this value is not a string then all truncated values will be replaced with undefined |
MIT