{"id":18605,"date":"2013-09-13T08:47:48","date_gmt":"2013-09-13T06:47:48","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=18605"},"modified":"2013-09-13T08:48:25","modified_gmt":"2013-09-13T06:48:25","slug":"beautifying-phps-json_encode-output","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2013\/09\/13\/beautifying-phps-json_encode-output\/","title":{"rendered":"Beautifying PHP&#8217;s json_encode() output"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve been working a bit more with PHP and JSON recently and one of the things that annoyed me quite a bit was the single line output of the <a href=\"http:\/\/php.net\/manual\/en\/function.json-encode.php\">json_encode()<\/a> function. \u00a0Here is an example:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$data = array(\r\n'foo' =&gt; 'bar',\r\n'bar' =&gt; 'qux',\r\n'blah' =&gt; 'blah',\r\n);\r\n\r\necho json_encode($data);\r\n?&gt;\r\n<\/pre>\n<p>Poorly readable result (imagine having larger, more complex data structures like nested arrays):<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n{&quot;foo&quot;:&quot;bar&quot;,&quot;bar&quot;:&quot;qux&quot;,&quot;blah&quot;:&quot;blah&quot;}\r\n<\/pre>\n<p>Apparently, since PHP 5.4.0 it became much easier to optionally format the output with some indentation. \u00a0<em>json_encode()<\/em> function has an $options parameter, which was given an extra option &#8211; <strong>JSON_PRETTY_PRINT<\/strong>. \u00a0Here is an updated example:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n$data = array(\r\n 'foo' =&gt; 'bar',\r\n 'bar' =&gt; 'qux',\r\n 'blah' =&gt; 'blah',\r\n);\r\n\r\necho json_encode($data, JSON_PRETTY_PRINT);\r\n?&gt;\r\n<\/pre>\n<p>And an updated output:<\/p>\n<pre class=\"brush: plain; light: true; title: ; notranslate\" title=\"\">\r\n{\r\n &quot;foo&quot;: &quot;bar&quot;,\r\n &quot;bar&quot;: &quot;qux&quot;,\r\n &quot;blah&quot;: &quot;blah&quot;\r\n}\r\n<\/pre>\n<p>Very handy for debugging and for those bits of JSON that should be editable by hand.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve been working a bit more with PHP and JSON recently and one of the things that annoyed me quite a bit was the single line output of the json_encode() function. \u00a0Here is an example: &lt;?php $data = array( &#8216;foo&#8217; =&gt; &#8216;bar&#8217;, &#8216;bar&#8217; =&gt; &#8216;qux&#8217;, &#8216;blah&#8217; =&gt; &#8216;blah&#8217;, ); echo json_encode($data); ?&gt; Poorly readable result &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2013\/09\/13\/beautifying-phps-json_encode-output\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Beautifying PHP&#8217;s json_encode() output<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62],"tags":[1139,38],"keyring_services":[],"class_list":["post-18605","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","tag-javascript","tag-php"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":27408,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/","url_meta":{"origin":18605,"position":0},"title":"Validating JSON against schema in PHP","author":"Leonid Mamchenkov","date":"March 11, 2017","format":false,"excerpt":"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). \u00a0Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=\"true\"] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27279,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/01\/30\/sharing-constants-between-php-classes\/","url_meta":{"origin":18605,"position":1},"title":"Sharing constants between PHP classes","author":"Leonid Mamchenkov","date":"January 30, 2017","format":false,"excerpt":"When writing larger applications, it is often useful to have some constants defined, which can then be shared between different parts of the application. \u00a0There are several ways to do this, and there is no real rocket science here. However, the question is: what's the best way to do so?\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11461,"url":"https:\/\/mamchenkov.net\/wordpress\/2008\/12\/12\/perl-vs-php-variable-scoping\/","url_meta":{"origin":18605,"position":2},"title":"Perl vs. PHP : variable scoping","author":"Leonid Mamchenkov","date":"December 12, 2008","format":false,"excerpt":"I've mentioned quite a few times that I am a big fan of Perl programming languge.\u00a0 However, most of my programming time these days is spent in PHP.\u00a0 The languages are often similar, with PHP having its roots in Perl, and Perl being such a influence in the world of\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16602,"url":"https:\/\/mamchenkov.net\/wordpress\/2012\/08\/03\/metasyntactic-variable\/","url_meta":{"origin":18605,"position":3},"title":"Metasyntactic variable","author":"Leonid Mamchenkov","date":"August 3, 2012","format":"link","excerpt":"Metasyntactic variable A \"standard list of metasyntactic variables used in syntax examples\" often used in the United States is: foo, bar, baz, qux, quux, corge, grault, garply, waldo, fred,\u00a0plugh,\u00a0xyzzy, thud.\u00a0The word\u00a0foo\u00a0occurs in over 330\u00a0RFCs\u00a0and\u00a0bar\u00a0occurs in over 290.\u00a0[...] Due to English being the foundation-language, or\u00a0lingua franca, of most computer programming languages\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12148,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/17\/cakephp-building-factories-with-models-and-behaviors\/","url_meta":{"origin":18605,"position":4},"title":"CakePHP : Building factories with models and behaviors","author":"Leonid Mamchenkov","date":"February 17, 2010","format":false,"excerpt":"CakePHP is a wonderful framework. \u00a0 Recently I proved it to myself once again (not that I need much of that proof anyway). \u00a0The problem that we had at work was a whole lot of code in once place and no obvious way of how to break that code into\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12138,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/12\/php-variables-strings-and-curly-braces\/","url_meta":{"origin":18605,"position":5},"title":"PHP variables, strings, and curly braces","author":"Leonid Mamchenkov","date":"February 12, 2010","format":false,"excerpt":"For the last couple of days we had a number arguments at work about what is the best way to surround a complex PHP variable inside a double-quoted string. \u00a0More specifically, should the sigil ($, dollar sign) be on the inside of the braces or on the outside. \u00a0Consider an\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/18605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=18605"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/18605\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=18605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=18605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=18605"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=18605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}