How do I write a pretty printed json in PHP?
I am creating an API in php. This API processes json messages from third party API.
I want to write invalid json printable messages.
So, I did this:
error_log("test\n" . json_encode($json_string, JSON_PRETTY_PRINT));
However, when I look at my logs, the json line is pretty unprinted:
$ tailf error.log
2015-07-13 10:20:03: (mod_fastcgi.c.2701) FastCGI-stderr: test
"{\"info\":{\"status\":200,\"msg\":\"OK\"},\"response\":{\"foo\":\"bar\"}"
I want to see something like:
$ tailf error.log
2015-07-13 10:20:03: (mod_fastcgi.c.2701) FastCGI-stderr: test
{
"info": {
"status": 200,
"msg ": "OK"
},
"response": {
"foo": "bar"
}
}
How can I achieve this result?
source to share
error_log("test\n" . json_encode($json_string, JSON_PRETTY_PRINT));
json_encode()
doesn't actually necessarily result in JSON: it will produce something that can be read by javascript. If you give it an array or object, it will create JSON; if you give it a string it will create a javascript string. And this is what you do, so this is what you get.
To be clear, $json_string
this is a string: (as far as PHP is concerned, this is a string, if you passed this same string to javascript it will be interpreted as an object). You pass this through json_encode()
and all you are about to get is another string (double JSON encoded string).
JSON_PRETTY_PRINT
does not work here because you are not creating JSON: you are creating what javascript will see as a string too.
Savvy?
So what you need to do is (a) convert it $json_string
back to a PHP array, and then (b) recode that as JSON, this time using a flag JSON_PRETTY_PRINT
.
$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);
Instead of converting it back to a PHP array and then reverting back to JSON, it would be better to add a flag JSON_PRETTY_PRINT
wherever you might get $json_string
it if possible.
Alternatively, just log $json_string
directly (no need to encode it: its already a string, you can pass it in error_log()
as it is), and only worry about overcoming it when you need to read your logs. This will cut your logs significantly.
source to share
General unix error logs should not contain human readable json or other unescaped characters. Many syslog / logging implementations are limited in character width and automatically add encoding (for example \"
) or remove new string characters, PHP error_log is also not binary safe - behavior when encountering a unicode character is unpredictable (not sure).
You shouldn't use your own log / error log functions instead, create your own json dedicated logger.
Personally, I use MongoDB to register json because it needs to work with MongoDB data.
source to share
In this case, you have two options:
if you can use str_replace like:
error_log("test\n" . str_replace('\"',"\n",json_encode($json_string, JSON_PRETTY_PRINT)));
or as @Karoly Horvath said:
You are encoding a string already encoded to JSON. Your $ json_string is already encoded. So you need to decode your first JSON and re-encode it with good parameters
error_log("test\n" . json_encode(json_decode($json_string), JSON_PRETTY_PRINT));
and give credit to @Caroli.
source to share