16

Does jQuery have a JSON/Javascript object to HTML pretty print function similar to PHP's var_dump? If yes, what is it?

ThinkingStiff
65.4k31 gold badges148 silver badges241 bronze badges
asked May 4, 2010 at 19:41
3

3 Answers 3

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)

answered May 4, 2010 at 19:44
Sign up to request clarification or add additional context in comments.

Comments

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"
 }
 }
 ]
}
*/
answered May 11, 2015 at 21:06

3 Comments

The question was to pretty print HTML, not to pretty print JSON.
See the note below my answer.
I know this isn't the answer to the question but I like this rather than having to use some other dependency.
-2

Using Jquery, you can have object.serialize() to output an object. This is similar to var_dump() in php or Zend_Debug::dump() in Zend.

CoolBeans
20.9k10 gold badges88 silver badges102 bronze badges
answered Jun 23, 2011 at 17:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.