Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As per <a href="https://developer.mozilla.org/En/Using_native_JSON" rel="noreferrer">the JSON docs at Mozilla</a>, <code>JSON.Stringify</code> has a second parameter <code>censor</code> which can be used to filter/ignore children items while parsing the tree. However, perhaps you can avoid the circular references.</p> <p>In Node.js we cannot. So we can do something like this:</p> <pre><code>function censor(censor) { var i = 0; return function(key, value) { if(i !== 0 &amp;&amp; typeof(censor) === 'object' &amp;&amp; typeof(value) == 'object' &amp;&amp; censor == value) return '[Circular]'; if(i &gt;= 29) // seems to be a harded maximum of 30 serialized objects? return '[Unknown]'; ++i; // so we know we aren't using the original object anymore return value; } } var b = {foo: {bar: null}}; b.foo.bar = b; console.log("Censoring: ", b); console.log("Result: ", JSON.stringify(b, censor(b))); </code></pre> <p>The result:</p> <pre><code>Censoring: { foo: { bar: [Circular] } } Result: {"foo":{"bar":"[Circular]"}} </code></pre> <p>Unfortunately there seems to be a maximum of 30 iterations before it automatically assumes it's circular. Otherwise, this should work. I even used <code>areEquivalent</code> <a href="https://stackoverflow.com/questions/1068834/object-comparison-in-javascript">from here</a>, but <code>JSON.Stringify</code> still throws the exception after 30 iterations. Still, it's good enough to get a decent representation of the object at a top level, if you really need it. Perhaps somebody can improve upon this though? In Node.js for an HTTP request object, I'm getting:</p> <pre><code>{ "limit": null, "size": 0, "chunks": [], "writable": true, "readable": false, "_events": { "pipe": [null, null], "error": [null] }, "before": [null], "after": [], "response": { "output": [], "outputEncodings": [], "writable": true, "_last": false, "chunkedEncoding": false, "shouldKeepAlive": true, "useChunkedEncodingByDefault": true, "_hasBody": true, "_trailer": "", "finished": false, "socket": { "_handle": { "writeQueueSize": 0, "socket": "[Unknown]", "onread": "[Unknown]" }, "_pendingWriteReqs": "[Unknown]", "_flags": "[Unknown]", "_connectQueueSize": "[Unknown]", "destroyed": "[Unknown]", "bytesRead": "[Unknown]", "bytesWritten": "[Unknown]", "allowHalfOpen": "[Unknown]", "writable": "[Unknown]", "readable": "[Unknown]", "server": "[Unknown]", "ondrain": "[Unknown]", "_idleTimeout": "[Unknown]", "_idleNext": "[Unknown]", "_idlePrev": "[Unknown]", "_idleStart": "[Unknown]", "_events": "[Unknown]", "ondata": "[Unknown]", "onend": "[Unknown]", "_httpMessage": "[Unknown]" }, "connection": "[Unknown]", "_events": "[Unknown]", "_headers": "[Unknown]", "_headerNames": "[Unknown]", "_pipeCount": "[Unknown]" }, "headers": "[Unknown]", "target": "[Unknown]", "_pipeCount": "[Unknown]", "method": "[Unknown]", "url": "[Unknown]", "query": "[Unknown]", "ended": "[Unknown]" } </code></pre> <p>I created a small Node.js module to do this here: <a href="https://github.com/ericmuyser/stringy" rel="noreferrer">https://github.com/ericmuyser/stringy</a> Feel free to improve/contribute!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload