We can pretty print JSON String in PHP using the json_encode() function and by making use of the JSON_PRETTY_PRINT option along with it.
Let's take a look by an example.
Unformatted JSON String
{"countries":[{"name":"Japan","cities":[{"name":"Tokyo","area":"Kanto"},{"name":"Osaka","area":"Kansai"}]},{"name":"China","cities":[{"name":"Beijing","area":"Beijing Municipality"},{"name":"Shanghai","area":"Shanghai Municipality"}]},{"name":"Australia","cities":[{"name":"Sydney","area":"New South Wales"},{"name":"Melbourne","area":"Victoria"}]}]}
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!