Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pretty-print JSON in a web page using JavaScript?

Seems to me that pretty-printing JSON is a simple enough task that JavaScript should be able to handle it. Has anyone ever written (or run across) a JavaScript function to do this?

like image 908
Hot Licks Avatar asked Jan 19 '11 19:01

Hot Licks


1 Answers

An easy way to do this is to execute:

JSON.stringify(data, null, "  ");

where data is the json object you want to print pretty.

Not every browser contains JSON though. You can include json.js by Douglas Crockford which add global JSON object if it is not supported natively by the browser.

like image 168
mgamer Avatar answered Sep 19 '22 11:09

mgamer