Beautyfying HTML/ Strings


Is there anyway in PHP (or in JS) you can beautify or properly indent the following jSON code?

I tried wrapping them up using “<Pre>” tags but it did not work. When I say beautify i mean the below code should be converted to something like this

{"result":{"64":{"id":64,"status":"sent","sent":10,"received":10,"remaining":0,"unique_views":8,"viewed":10,"clicked":0,"clicker":0,"hard_bounce":0,"soft_bounce":0,"unsub":0,"complaints":0,"listid":[140],"campaign_name":"18:22","tags":"","subject":"18:22","from_name":"[DEFAULT_FROM_NAME]","from_email":"[DEFAULT_FROM_EMAIL]","reply_to":"[DEFAULT_REPLY_TO]","stats_by_domain":{"":{"sent":10,"received":null,"viewed":10,"clicked":0,"hard_bounce":0,"unsub":0,"complaints":0}},"scheduled_date":"2013-02-28 07:00:00","bat_sent":"yes"}}}

Thanks for any help.

Depending on which browser you use you can get JSON prettify plugins which format the JSON in real time when you dump the source to the browser.

For testing purposes i often copy json strings into the validator which formats the json as your describing.

No, i am actually looking for a PHP/JS plugin or snippet so that i can add it to my code.


Its not worth it, for testing purposes its more beneficial to use a browser tool which is dynamic for every project that you use JSON in compared to using a server side parser which can lag your script.

i was taking a look into and it seems they are using some JS to format the JSON properly. Can I not use something like that? And since its JS to it wont be an overhead for the server.

For what it’s worth, as of PHP 5.4.0 there is an option to the json_encode() function called JSON_PRETTY_PRINT which will indent the JSON for you.

Here’s an example with your JSON.