Does jQuery have a JSON/Javascript object to HTML pretty print function similar to PHP's var_dump? If yes, what is it?
Asked
Active
Viewed 2.7k times
16
-
possible duplicate of http://stackoverflow.com/questions/323517/is-there-an-equivalent-for-var-dump-php-in-javascript – artlung May 04 '10 at 19:56
-
1I looked at that before I posted. – Fletcher Moore May 04 '10 at 20:13
-
Take a look at this SO Post http://stackoverflow.com/questions/323517/is-there-an-equivalent-for-var-dump-php-in-javascript – Chris Wagner May 04 '10 at 19:43
3 Answers
24
jQuery does not (out of the box).
However, James Padolsey created this prettyPrint which I really like.
Also, if you're using Firebug or Web Inspector (or similar), you can just type the object into the console, press return, and see a tree-dump of the object. To force a tree-view, call console.dir(obj)

Matt
- 43,482
- 6
- 101
- 102
4
Although the accepted answer is correct that jQuery does not have a pretty print feature for JSON, that feature is now included in out of the box javascript through JSON.stringify()'s
space argument.
To print to HTML, wrapping the output with <pre> </pre>
will preserve the line spacing for readability purposes.
var obj = {a:1, 'b':'foo', c:[false,'false',null, 'null', {d:{e:1.3e5,f:'1.3e5'}}]};
var str = "<pre>" + JSON.stringify(obj, undefined, 4) + "</pre>";
/* Returns
{
"a": 1,
"b": "foo",
"c": [
false,
"false",
null,
"null",
{
"d": {
"e": 130000,
"f": "1.3e5"
}
}
]
}
*/

Michael.Lumley
- 2,345
- 2
- 31
- 53
-
2
-
-
I know this isn't the answer to the question but I like this rather than having to use some other dependency. – cjones26 Mar 13 '19 at 18:18