Pretty-Printing JSON with PHP
PHP 5.4 offers the JSON_PRETTY_PRINT
option for use with the json_encode()
call.
https://php.net/manual/en/function.json-encode.php
<?php
...
$json_string = 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 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;
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>';
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.
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);
How to do jscon encode :: pretty print and unescaped slashes at same time in php
json_encode takes options as a bit mask. You can use multiple with the | character. So try:
$v = json_encode($arr, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
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..
Related Topics
How to Make MySQLi Connect Function
Shuffles Random Numbers with No Repetition in JavaScript/Php
How to Change Default Redirect Url of Laravel 5 Auth Filter
Forbidden :You Don't Have Permission to Access /Phpmyadmin on This Server
Transpose Multidimensional Array and Join Values with Commas
Install Yii2 Extension Manually Without Using Composer
How to Only Use Created_At in Laravel
Get the Site Status - Up or Down
Laravel 5.1 Unknown Database Type Enum Requested
Test PHP's Mail Function from Localhost
How to Store Configuration Variables in PHP
Supplied Argument Is Not a Valid MySQL Result Resource
Expose "Use" Classes to Included File
Check for Session Timeout in Laravel
Set Application_Env via Virtual Host Config and Read This in PHP
File Not Found When Running PHP with Nginx