JSON.stringify() pretty printer
I normally format my JSON when debugging in python with:
logger.debug('my stuff: %s' % json.dumps(my_json, indent=4))
But I found out you can do the same in Javascript by adding a couple of extra parameters to the JSON.stringify() function, like:
console.log('my stuff: ' + JSON.stringify(my_json, undefined, 4));
More at: https://developer.mozilla.org/en-US/docs/Using_native_JSON
Written by Doug Thomas
Related protips
1 Response
I discovered that recently too, pretty neat!
What I think would be cool is another parameter that specifies the initial indent of pretty-printed json block. Would be useful if I want to output the result of JSON.stringify inside an already existing piece of code... Anybody knows of an easy way to do this?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#