Pretty-Printing JSON with PHP
PHP 5.4 offers the JSON_PRETTY_PRINT
option for use with the json_encode()
call.
http://php.net/manual/en/function.json-encode.php
<?php
...
$json_string = json_encode($data, JSON_PRETTY_PRINT);
How to pretty print JSON with wp_json_encode()?
You can make use of wp_json_encode() options parameter.
Setting options to JSON_PRETTY_PRINT
as an argument will make the function use whitespace in returned data to format it:
wp_json_encode($data, JSON_PRETTY_PRINT);
PHP pretty print json_encode
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 ;)
JSON pretty print without use json_decode in php
Only a parser can understand the JSON, so you can either do what you proposed or write your own parser. If you have access to the origin of the JSON, make it pretty in the first place.
Pretty print JSON that's already encoded in PHP
json_encode($json, JSON_PRETTY_PRINT);
Save it like that then display it with <pre>
and <code>
(if needed) tags wrapped around it..
Json encode PRETTY PRINT printing out with slashes in php
The data you get from wp_remote_get
is already a perfectly encoded JSON string, no need to encode again.
When you encode again, PHP generates a structure with one element, the string you originally get, and escapes all the quotes to form valid JSON.
So you can output the data directly :
$response = wp_remote_get( 'URL TO JSON DATA' ));
header('Content-Type: application/json');
echo $response;
How to log pretty printed json in PHP?
error_log("test\n" . json_encode($json_string, JSON_PRETTY_PRINT));
json_encode()
will actually not necessarily produce JSON: it will produce something that can be read by javascript. If you give it an array or an object, it will produce JSON; if you give it a string, it will produce a javascript string. And that’s what you’re doing, so that’s what you’re getting.
To be clear, $json_string
is a string: (as far as PHP is concerned, it’s a string; if you passed that same string to javascript, it would be interpreted as an object). You pass that through json_encode()
and all you’re going to get is another string (a string of doubly-encoded JSON).
JSON_PRETTY_PRINT
is having no effect here, because you’re not producing JSON: you’re producing something that javascript too would see as a string.
Savvy?
So what you need to do is to (a) turn $json_string
back into a PHP array, and then (b) reencode that as JSON, this time using the JSON_PRETTY_PRINT
flag.
$log_array = json_decode($json_string, true);
$json_pretty_string = json_encode($log_array, JSON_PRETTY_PRINT);
error_log('test' . PHP_EOL . $json_pretty_string);
Rather than converting it back to a PHP array and then back to JSON, it would be better to add the JSON_PRETTY_PRINT
flag to wherever you’re getting $json_string
from in the first place, if possible.
Alternatively, just log $json_string
directly (no need to encode it: it’s already a string, you can pass it to error_log()
as it is), and worry about prettifying it only when you need to read your logs. This will make your logs considerably smaller.
Why the code is not printing in Pretty Json format?
You just need to add a second a parameter to json_encode()
and add <pre>
tag. Like this:
echo '<pre>';
echo json_encode($data, JSON_PRETTY_PRINT);
echo '</pre>';
Related Topics
Is There a Function to Extract a 'Column' from an Array in PHP
How to Make Asynchronous Http Requests in PHP
Passing Base64 Encoded Strings in Url
Compare 2-Dimensional Data Sets Based on a Specified Second Level Value
How to Remove "Index.PHP" in Codeigniter'S Path
PHP Pass by Reference in Foreach
Difference Between Public, Private, and Protected
How to Sort an Array of Associative Arrays by Value of a Given Key in PHP
Fatal Error: Maximum Execution Time of 30 Seconds Exceeded
MySQL VS MySQLi When Using PHP
How to Get Multiple Selected Values of Select Box in PHP
What Does the Variable $This Mean in PHP