util.inspect - JSON compliant

2,114 views
Skip to first unread message

metamind

unread,
Dec 1, 2010, 3:41:54 AM12/1/10
to nodejs
Hi,

It would be great if util.inspect could have a flag that would output
the results in a JSON compliant manner (putting quote marks around the
strings). I am inspecting some big objects and I would like to be able
to put the util.inspect output into a JSON object browser (such as the
FF extension JSONView)

Isaac Schlueter

unread,
Dec 1, 2010, 4:10:09 AM12/1/10
to nod...@googlegroups.com
If you have a JSON pretty-printer, why not just use JSON.stringify(myobj)?

--i

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

Darklord

unread,
Dec 1, 2010, 3:57:30 AM12/1/10
to nod...@googlegroups.com
You could use JSON.stringify it outputs an object as proper json.

Sent from my iPhone

metamind

unread,
Dec 1, 2010, 5:08:18 AM12/1/10
to nodejs
it choked on circular refs.

Konstantin Käfer

unread,
Dec 1, 2010, 5:36:54 AM12/1/10
to nod...@googlegroups.com
JSON does not support circular references.

On Wed, Dec 1, 2010 at 12:08, metamind <no...@craft-e.com> wrote:
> it choked on circular refs.

YANG Xudong

unread,
Dec 1, 2010, 5:37:07 AM12/1/10
to nod...@googlegroups.com
How would you expect a JSONifier that doesn't choke on circular refs to work? Like, what is the 'non-choking' way to stringify A after executing 'a={};a.a=a;'?
=======================
杨旭东 YANG Xudong "Wyverald"
Juntos, unidos, triunfará nuestro deseo de ser el mejor
alias please=sudo
please rm -rf /



On Wed, Dec 1, 2010 at 18:08, metamind <no...@craft-e.com> wrote:
it choked on circular refs.

Jorge

unread,
Dec 1, 2010, 6:16:36 AM12/1/10
to nod...@googlegroups.com
On 01/12/2010, at 11:37, YANG Xudong wrote:

> How would you expect a JSONifier that doesn't choke on circular refs to work?
> Like, what is the 'non-choking' way to stringify A after executing 'a={};a.a=a;'?

For sys.inspect() ( now util.inspect() ) I'd like it to be so:

{ a: this } , { a: self } , { a: c'est } , { a: esto } , { a: das } ...

a= { a: [ {}, 27 ] }; a.b= a.a; a.c= a.a[0]; a.d= a.a[1];

{ a: [ {}, 27 ], b: this.a, c: this.a[0], d: 27 }
--
Jorge.

YANG Xudong

unread,
Dec 1, 2010, 6:18:39 AM12/1/10
to nod...@googlegroups.com
That AFAIK is not legal JSON.

=======================
杨旭东 YANG Xudong "Wyverald"
Juntos, unidos, triunfará nuestro deseo de ser el mejor
alias please=sudo
please rm -rf /




--

Jorge

unread,
Dec 1, 2010, 6:26:50 AM12/1/10
to nod...@googlegroups.com
On 01/12/2010, at 12:18, YANG Xudong wrote:
> On Wed, Dec 1, 2010 at 19:16, Jorge <jo...@jorgechamorro.com> wrote:
>>
>> For sys.inspect() ( now util.inspect() ) I'd like it to be so:
>>
>> { a: this } , { a: self } , { a: c'est } , { a: esto } , { a: das } ...
>>
>> a= { a: [ {}, 27 ] }; a.b= a.a; a.c= a.a[0]; a.d= a.a[1];
>>
>> { a: [ {}, 27 ], b: this.a, c: this.a[0], d: 27 }
>
> That AFAIK is not legal JSON.

No, it isn't, but sys^H^H^Hutil.inspect() is not a JSON.stringify()er...
--
Jorge.

metamind

unread,
Dec 1, 2010, 6:56:50 AM12/1/10
to nodejs
also, the good thing about inspect is that it doesn't bother going
right to the leaves of every object, you can specify the depth of
reporting you want.

Marco Rogers

unread,
Dec 1, 2010, 10:15:15 AM12/1/10
to nodejs
This came up just recently. I guess I understand why people need
this. metamind's use case is the most relevant I've seen so far. But
maybe you should move away from discussing it in terms of
util.inspect. The purpose of that function is not to produce
actionable data. It's not even geared towards JSON in particular.
It's for any javascript object, and it produces a representation of
the object that is geared towards a human "inspecting" it. An
implementation of JSON.stringify that handles circular references is
something different entirely. Though you could use the inspect code
as a basis. It wouldn't be difficult to throw it in a userland module
and modify the output to be more JSON-y.

FYI, I was curious how browser console's handle circular objects.
Short answer is they don't. They'll represent each circular reference
as a separate object that can be descended into infinitely. But it
does so lazily so it doesn't blow the stack. Whatever json browser
you use, make sure it's that smart :)

:Marco

metamind

unread,
Dec 1, 2010, 11:46:35 AM12/1/10
to nodejs
The way inspect handles circular refs is fine (it just says that the
ref is circular). Inspect results can be v. long and so its easy to
loose your place wrt indentation (especially with all the functions
being enumerated). I just wanted to be able to copy the console output
into a text file, point a browser at it then collapse some of the
larger objects that I'm not interested in.

Isaac Schlueter

unread,
Dec 1, 2010, 12:15:25 PM12/1/10
to nod...@googlegroups.com
util.inspect will never be JSON compatible. It allows functions and
circular links, which browsers won't know how to parse. It's not even
valid JavaScript, because it's for humans, not machines.

JSONView can't collapse non-JSON output. If you want JSON compatible
output, use JSON.stringify.

If you want it to handle circular links and other odd things in a
different way, then provide a translator function.

JSON.stringify(myCrazyObject, translatorFunction, indentation)

--i

Marco Rogers

unread,
Dec 1, 2010, 12:37:32 PM12/1/10
to nodejs
I think that's what I said. Maybe with a little less authority.
Forgot about the stringify translator function too.

On Dec 1, 12:15 pm, Isaac Schlueter <i...@izs.me> wrote:
> util.inspect will never be JSON compatible.  It allows functions and
> circular links, which browsers won't know how to parse.  It's not even
> valid JavaScript, because it's for humans, not machines.
>
> JSONView can't collapse non-JSON output.  If you want JSON compatible
> output, use JSON.stringify.
>
> If you want it to handle circular links and other odd things in a
> different way, then provide a translator function.
>
>     JSON.stringify(myCrazyObject, translatorFunction, indentation)
>
> --i
>

Tim Caswell

unread,
Dec 1, 2010, 12:45:10 PM12/1/10
to nod...@googlegroups.com
It sounds like what is wanted is a interactive inspector. There are a
few clients that use V8's debugging protocol. I know of one for
Eclipse as well as a few that run in the browser. (node-inspect, Ares
Debugger, Cloud 9 IDE). Then you can grab values and inspect them
interactively the same way you do for browser-side code.

Dominic Tarr

unread,
Dec 1, 2010, 8:55:17 PM12/1/10
to nod...@googlegroups.com
i know substack's dnode handles circular references.

but I don't know of the top of my head whether it's stringify/parse is available as a standalone package

Alexey Petrushin

unread,
Nov 12, 2012, 12:18:41 AM11/12/12
to nod...@googlegroups.com
Have similar need, I store some JSON data in files, and in order to be able to inspect it the JSON generated with pretty print option. The problem though - it's too bloated, util.inspect has much nicer format.

    var obj = {a: [1, 2, 3]}
    
inspect will print

    { a: [ 1, 2, 3 ] }
    
JSON.stringify

    {
      "a": [
        1,
        2,
        3
      ]
    }
Reply all
Reply to author
Forward
0 new messages