
Security News
Official Go SDK for MCP in Development, Stable Release Expected in August
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
pruning JSON.stringify for the very specific cases where you need to stringify big or recursive javascript objects and don't really need the result to be complete
JSON.prune is a pruning JSON.stringify
for the very specific cases where you need to stringify big or recursive javascript objects and don't really need the result to be complete.
var json = JSON.stringify(window); // fails
var json = JSON.prune(window); // builds a JSON valid string from a pruned version of the
// recursive, deep, and not totally accessible window object
var prunedWindow = JSON.parse(JSON.prune(window)); // builds a lighter acyclic version of window
JSON.prune also lets you, in case of need, stringify inherited and/or non enumerable properties.
JSON.prune(window.location, {inheritedProperties:true}); // without inherited properties, FireFox and IE only show an empty object
It's totally useless for at least 99% of js developpers.
JSON.prune.log is a proxy over console.log deep cloning the objects (using JSON.prune) before logging them, in order to avoid the delay problem encountered on non primitive objects logging.
You should not use it frequently, only when you really need to see the objects how they were at logging time.
// make sure someObject is logged as it was at logging time
JSON.prune.log(someObject);
<script src=http://dystroy.org/JSON.prune/JSON.prune.js></script>
var prune = require('json-prune');
var json = prune(obj);
Here's how are handled by default by JSON.prune the special values needing pruning:
Value | Default |
---|---|
undefined | Key and value are ommited (same as JSON.stringify ) |
function | Key and value are ommited (same as JSON.stringify ) |
already written or too deep object (cycle prevention) | The "-pruned-" string |
array with too many elements | Truncation: JSON.prune applied to only the start of the array |
By specifiying a replacer
or a prunedString
in JSON.prune
options, you can customize those prunings.
The replacer
function takes 3 arguments:
Returning undefined
makes JSON.prune
ommit the property (name and value).
The default value makes it easy to just specify the specific behavior you want instead of implementing the whole standard replacement.
var json = JSON.prune(obj, {prunedString: '{}' });
Note: if you want a string to be inserted, don't forget the double quotes, as in '"-pruned-"'
.
If you want the pruned properties to just be ommited, pass undefined
as prunedString
:
var obj = {a:3};
obj.self = obj;
var json = JSON.prune(obj);
console.log(json); // logs {"a":3,"self":"-pruned-"}
json = JSON.prune(obj, {prunedString: undefined });
console.log(json); // logs {"a":3}
Note: You get the same behavior with
json = JSON.prune(obj, {replacer: function(){}});
var options = {replacer:function(value, defaultValue, circular){
if (circular) return '"-circular-"';
if (value === undefined) return '"-undefined-"';
if (Array.isArray(value)) return '"-array('+value.length+')-"';
return defaultValue;
}};
var json = JSON.prune(obj, options);
var options = {replacer:function(value, defaultValue){
if (typeof value === "function") return JSON.stringify(value.toString());
return defaultValue;
}};
var json = JSON.prune(obj, options);
The default behavior on big arrays is to silently write only the first elements. It's possible with a replacer
to add a string as last element:
var obj = {arr: Array.apply(0,Array(100)).map(function(_,i){ return i+1 })}
function replacer(value, defaultValue){
if (Array.isArray(value)) return defaultValue.replace(/]$/, ',"-truncated-"]');
return defaultValue;
}
var json = (asPrunedJSON(obj, {arrayMaxLength:5, replacer});
This produces
{"arr":[1,2,3,4,5,"-truncated-"]}
MIT
FAQs
pruning JSON.stringify for the very specific cases where you need to stringify big or recursive javascript objects and don't really need the result to be complete
The npm package json-prune receives a total of 5,187 weekly downloads. As such, json-prune popularity was classified as popular.
We found that json-prune demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.
Security News
New research reveals that LLMs often fake understanding, passing benchmarks but failing to apply concepts or stay internally consistent.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.