Last Updated: February 25, 2016
·
15.15K
· douglas-c-thomas

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

1 Response
Add your 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 ·