{"id":1933,"date":"2017-03-23T19:48:05","date_gmt":"2017-03-23T14:18:05","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1933"},"modified":"2017-03-28T17:25:10","modified_gmt":"2017-03-28T11:55:10","slug":"pretty-printing-json-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/pretty-printing-json-php\/","title":{"rendered":"[ Solved &#8211; 7 Answers ] PHP &#8211; Pretty-Printing JSON with PHP"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM :<\/label><\/p>\n<p>We build a PHP script that feeds JSON data to another script. \u00a0ur script builds data into a large associative array, and then outputs the data using json_encode.<\/p>\n<h4 id=\"here-is-an-example-script\"><strong><span style=\"color: #ff6600;\">Here is an example script:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24data%20%3D%20array(\u2018a\u2019%20%3D%3E%20\u2019apple\u2019%2C%20\u2019b\u2019%20%3D%3E%20\u2019banana\u2019%2C%20\u2019c\u2019%20%3D%3E%20\u2019catnip\u2019)%3B%0Aheader(\u2018Content-type%3A%20text%2Fjavascript\u2019)%3B%0Aecho%20json_encode(%24data)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>The above code yields the following output:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%7B%22a%22%3A%22apple%22%2C%22b%22%3A%22banana%22%2C%22c%22%3A%22catnip%22%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>This is great if you have a small amount of data, but I\u2019d prefer something along these lines:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%7B%0A%20%20%20%20%22a%22%3A%20%22apple%22%2C%0A%20%20%20%20%22b%22%3A%20%22banana%22%2C%0A%20%20%20%20%22c%22%3A%20%22catnip%22%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Is there a way to do this in PHP without an ugly hack? It seems like someone at Facebook figured it out.<\/li>\n<li>php json pretty-print<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>PHP 5.4 offers the JSON_PRETTY_PRINT option for use with the json_encode() call.<\/p>\n<p>http:\/\/php.net\/manual\/en\/function.json-encode.php<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A\u2026%0A%24json_string%20%3D%20json_encode(%24data%2C%20JSON_PRETTY_PRINT)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<p>This function will take JSON string and indent it very readable. It also should be convergent,<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dprettyPrint(%20%24json%20)%20%3D%3D%3D%20prettyPrint(%20prettyPrint(%20%24json%20)%20)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Input<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%7B%22key1%22%3A%5B1%2C2%2C3%5D%2C%22key2%22%3A%22value%22%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Output<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%7B%0A%20%20%20%20%22key1%22%3A%20%5B%0A%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%202%2C%0A%20%20%20%20%20%20%20%203%0A%20%20%20%20%5D%2C%0A%20%20%20%20%22key2%22%3A%20%22value%22%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"code\"><strong><span style=\"color: #ff6600;\">Code:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dfunction%20prettyPrint(%20%24json%20)%0A%7B%0A%20%20%20%20%24result%20%3D%20\u201d%3B%0A%20%20%20%20%24level%20%3D%200%3B%0A%20%20%20%20%24in_quotes%20%3D%20false%3B%0A%20%20%20%20%24in_escape%20%3D%20false%3B%0A%20%20%20%20%24ends_line_level%20%3D%20NULL%3B%0A%20%20%20%20%24json_length%20%3D%20strlen(%20%24json%20)%3B%0A%0A%20%20%20%20for(%20%24i%20%3D%200%3B%20%24i%20%3C%20%24json_length%3B%20%24i%2B%2B%20)%20%7B%0A%20%20%20%20%20%20%20%20%24char%20%3D%20%24json%5B%24i%5D%3B%0A%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%24post%20%3D%20%22%22%3B%0A%20%20%20%20%20%20%20%20if(%20%24ends_line_level%20!%3D%3D%20NULL%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20%24ends_line_level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20if%20(%20%24in_escape%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24in_escape%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20if(%20%24char%20%3D%3D%3D%20\u2019%22\u2019%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24in_quotes%20%3D%20!%24in_quotes%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20if(%20!%20%24in_quotes%20)%20%7B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dphp\u201d manual=\u201dswitch(%20%24char%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2019%7D\u2019%3A%20case%20\u2019%5D\u2019%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24level\u2013%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20%24level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2019%7B\u2019%3A%20case%20\u2019%5B\u2019%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24level%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2019%2C\u2019%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20%24level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20\u2019%3A\u2019%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24post%20%3D%20%22%20%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22%20%22%3A%20case%20%22%5Ct%22%3A%20case%20%22%5Cn%22%3A%20case%20%22%5Cr%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24char%20%3D%20%22%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24ends_line_level%20%3D%20%24new_line_level%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24new_line_level%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%20else%20if%20(%20%24char%20%3D%3D%3D%20\u2019%5C%5C\u2019%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24in_escape%20%3D%20true%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dphp\u201d manual=\u201d%20%7D%0A%20%20%20%20%20%20%20%20if(%20%24new_line_level%20!%3D%3D%20NULL%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24result%20.%3D%20%22%5Cn%22.str_repeat(%20%22%5Ct%22%2C%20%24new_line_level%20)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%24result%20.%3D%20%24char.%24post%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20return%20%24result%3B%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<h4 id=\"we-just-used-the-json-formatting-code-here\"><strong><span style=\"color: #808000;\">We just used the json formatting code here:<\/span><\/strong><\/h4>\n<p>http:\/\/recursive-design.com\/blog\/2008\/03\/11\/format-json-with-php\/<\/p>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201decho%20json_encode(%24results%2C%20JSON_PRETTY_PRINT)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Which is absolutely right. But it\u2019s not enough, the browser needs to understand the type of data, you can specify the header just before echo-ing the data back to the user.<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dheader(\u2018Content-Type%3A%20application%2Fjson\u2019)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>This will result in a well formatted output.<\/li>\n<li>Or, if you like extensions you can use JSONView for Chrome.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<h4 id=\"simple-way-for-php5-4\"><strong><span style=\"color: #800080;\">Simple way for php>5.4:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24Data%20%3D%20array(\u2018a\u2019%20%3D%3E%20\u2019apple\u2019%2C%20\u2019b\u2019%20%3D%3E%20\u2019banana\u2019%2C%20\u2019c\u2019%20%3D%3E%20\u2019catnip\u2019)%3B%0A%24json%3D%20json_encode(%24Data%2C%20JSON_PRETTY_PRINT)%3B%0Aheader(\u2018Content-Type%3A%20application%2Fjson\u2019)%3B%0Aprint_r(%24json)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Result in browser<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%7B%0A%20%20%20%20%22a%22%3A%20%22apple%22%2C%0A%20%20%20%20%22b%22%3A%20%22banana%22%2C%0A%20%20%20%20%22c%22%3A%20%22catnip%22%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<p>How to encode an associative array to a pretty-formatted JSON string, so this doesn\u2019t directly answer the question, but if you have a string that is already in JSON format, you can make it pretty simply by decoding and re-encoding it (requires PHP >= 5.4):<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24json%20%3D%20json_encode(json_decode(%24json)%2C%20JSON_PRETTY_PRINT)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"example\"><span style=\"color: #000000;\">Example:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24json_ugly%20%3D%20\u2019%7B%22a%22%3A1%2C%22b%22%3A2%2C%22c%22%3A3%2C%22d%22%3A4%2C%22e%22%3A5%7D\u2019%3B%0A%24json_pretty%20%3D%20json_encode(json_decode(%24json_ugly)%2C%20JSON_PRETTY_PRINT)%3B%0Aecho%20%24json_pretty%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"this-outputs\"><span style=\"color: #ff6600;\"><strong>This outputs:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%7B%0A%20%20%20%20%22a%22%3A%201%2C%0A%20%20%20%20%22b%22%3A%202%2C%0A%20%20%20%20%22c%22%3A%203%2C%0A%20%20%20%20%22d%22%3A%204%2C%0A%20%20%20%20%22e%22%3A%205%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<h4 id=\"use-pre-in-combination-with-json_encode-and-the-json_pretty_print-option\"><span style=\"color: #000000;\">Use <pre> in combination with json_encode() and the JSON_PRETTY_PRINT option:<\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3Cpre%3E%0A%20%20%20%20%3C%3Fphp%0A%20%20%20%20echo%20json_encode(%24dataArray%2C%20JSON_PRETTY_PRINT)%3B%0A%20%20%20%20%3F%3E%0A%3C%2Fpre%3E%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM : We build a PHP script that feeds JSON data to another script. \u00a0ur script builds data into a large associative array, and then outputs the data using json_encode. Here is an example script: [pastacode lang=\u201dphp\u201d manual=\u201d%24data%20%3D%20array(\u2018a\u2019%20%3D%3E%20\u2019apple\u2019%2C%20\u2019b\u2019%20%3D%3E%20\u2019banana\u2019%2C%20\u2019c\u2019%20%3D%3E%20\u2019catnip\u2019)%3B%0Aheader(\u2018Content-type%3A%20text%2Fjavascript\u2019)%3B%0Aecho%20json_encode(%24data)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] The above code yields the following output: [pastacode lang=\u201dphp\u201d manual=\u201d%7B%22a%22%3A%22apple%22%2C%22b%22%3A%22banana%22%2C%22c%22%3A%22catnip%22%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[4180,4173,4172,4178,4174,4179,4176,4175,4177,3885],"class_list":["post-1933","post","type-post","status-publish","format-standard","hentry","category-php","tag-alternative-layout-for-pretty-printed-json-string-structure","tag-can-comments-be-used-in-json","tag-how-can-i-pretty-format-my-json-output-in-ruby-on-rails","tag-how-can-i-pretty-print-json-using-javascript","tag-how-can-i-pretty-print-json","tag-how-to-log-pretty-printed-json-in-php","tag-is-there-a-pretty-print-for-php","tag-pretty-printing-xml-in-python","tag-pretty-print-json-in-java","tag-what-is-the-correct-json-content-type"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1933"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1933\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}