PHP "pretty print" json_encode
Solution 1
PHP has JSON_PRETTY_PRINT option since 5.4.0 (release date 01-Mar-2012).
This should do the job:
$json = json_decode($string);
echo json_encode($json, JSON_PRETTY_PRINT);
See http://www.php.net/manual/en/function.json-encode.php
Note: Don't forget to echo "<pre>" before and "</pre>" after, if you're printing it in HTML to preserve formatting ;)
Solution 2
Hmmm $array = json_decode($json, true);
will make your string an array which is easy to print nicely with print_r($array, true);
But if you really want to prettify your json... Check this out
Solution 3
Here's a function to pretty up your json: pretty_json
Solution 4
And for PHP 5.3, you can use this function, which can be embedded in a class or used in procedural style:
http://svn.kd2.org/svn/misc/libs/tools/json_readable_encode.php
Related videos on Youtube
Brian
Updated on July 08, 2022Comments
-
Brian almost 2 years
Possible Duplicate:
Pretty-Printing JSON with PHPI'm working on a script that creates a JSON file. Right now I'm just using
json_encode
(PHP 5.2.x) to encode an array into JSON output. Then I print the returned value to a file and save it. Problem is that the client wants to be able to open these JSON files for readability, so I'd like to add line breaks in and "pretty print" the JSON output. Any ideas on how to do this? My only other alternative that I can see is to not usejson_encode
at all and just write the file contents manually and add in my own line breaks for each line.Here's what I get:
{"product_name":"prod1","val1":1,"val2":8}
Here's what I want:
{ "product_name":"prod1", "val1":1, "val2":8 }
I suppose I could also just replace every comma with a command followed by a \n, and same for the brackets... Thoughts?
-
netcoder almost 13 years
-
Elangovan almost 9 years$jsonArr = '{"product_name":"prod1","val1":1,"val2":8}'; $jsonArr = json_decode($jsonArr); echo "<pre>";print_R($jsonArr);
-
-
sg3s almost 13 years@Michael Mior you should see my debugging die
die(print('<pre>'.print_r($var, true).'</pre>'))
it prints almost anything :p -
Michael Mior almost 13 yearsI usually just view the source and go with
var_dump
, but whatever works :) -
emilyk almost 11 yearsWorked perfectly to simply copy and paste into my php file, especially since I didn't want to do all the work of upgrading to PHP 5.4 on my mac. Thanks.
-
Nic Cottrell almost 10 yearsIn PHP <5.4 replace
JSON_PRETTY_PRINT
with 128 -
drzaus over 9 years@NicCottrell doesn't work when i test it here sandbox.onlinephpfunctions.com/code/…
-
petrkotek over 9 years@drzaus works for me there - I can see each key of JSON on separate line (the PHP version used on that site has even JSON_PRETTY_PRINT defined.
-
drzaus over 9 years@beret ahh, I thought sharing it would retain the PHP setting -- change the php version to anything less than 5.4 and it should go back to "unformatted"
-
petrkotek over 9 years@drzaus Oh I see. I didn't look for the switch of PHP version. Yeah, apparently not only the constant has been added, but mainly the functionality. Makes sense.
-
GisMofx over 8 yearsThank you for the
<pre>json</pre>
tip! -
kander about 8 yearsJust wondering how
brew install php54
is considered a lot of work. When doing development on a Mac, it's fairly likely you have homebrew installed... -
Octopus about 7 yearsInstead of using "<pre>" tags (which will render your JSON invalid), you ought to use
header("Content-Type: application/json")
and then your browser will render it as plain text rather than markup. -
Marco almost 3 yearsJust use 128 as commented by @Nic Cottrell.