Unable to print json with print_r using php and get value from command property
I have this json from my database and I want to extract one value from it:
{"uuid":"3ed79fdc-6ef0-441b-b9b0-525eeaebf183","displayName":"App\\Jobs\\SendReport","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\SendReport","command":"O:19:\"App\\Jobs\\SendReport\":11:{s:12:\"\u0000*\u0000tableName\";s:9:\"employees\";s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";N;s:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"}}
If I use a tool online to visualize jsons, it looks like this:
Then, first i want print the json using "print_r()" function of php and I can't I don't know why.
it does not return any value.
$string = '{"uuid":"3ed79fdc-6ef0-441b-b9b0-525eeaebf183","displayName":"App\\Jobs\\SendReport","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\SendReport","command":"O:19:\"App\\Jobs\\SendReport\":11:{s:12:\"\u0000*\u0000tableName\";s:9:\"employees\";s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";N;s:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"}}';
$payload = json_decode($string, true);
//print the json
print_r($payload);
//nothing happnes
//extract the table name:
$command = unserialize($payload['data']['command']);
echo $command->tableName;
//can't access to tableName property.
is possible extract that value from the json? please guys if you have any idea I will apreciate it. I'm using json_decode to transform that string to decode the json string and then I'm trying to access to one property and get the tableName 'employees' and I can't.
This is another payload but the table name is "departments":
$string = '{"uuid":"8c4640de-599c-4795-bffe-b043cf74a226","displayName":"App\\Jobs\\SendReport","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\Jobs\\SendReport","command":"O:19:\"App\\Jobs\\SendReport\":11:{s:12:\"\u0000*\u0000tableName\";s:11:\"departments\";s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";N;s:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"}}';
Here the key before 'departments' is s:11 in the first payload of which contains 'employees' there is a different key called s:9 I would like to know how get this value.
Thanks so much.
Answer
Solution:
This value is a mix of JSON and another form of serialization, which can be created with serialize() function. You can decode it by its opposite - unserialize().
Try to decode JSON first and then to unserialize that command
value (still assuming it's WordPress):
$json = '{"uuid":"3ed79fdc-6ef0-441b-b9b0-525eeaebf183","displayName":"App\\\Jobs\\\SendReport","job":"Illuminate\\\Queue\\\CallQueuedHandler@call","maxTries":null,"maxExceptions":null,"failOnTimeout":false,"backoff":null,"timeout":null,"retryUntil":null,"data":{"commandName":"App\\\Jobs\\\SendReport","command":"O:19:\"App\\\Jobs\\\SendReport\":11:{s:9:\"tableName\";s:9:\"employees\";s:3:\"job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:19:\"chainCatchCallbacks\";N;s:5:\"delay\";N;s:11:\"afterCommit\";N;s:10:\"middleware\";a:0:{}s:7:\"chained\";a:0:{}}"}}';
$item = json_decode($json);
$command = unserialize($item->data->command);
var_dump($command); // what's there?
tableName
is a protected property, so I'm guessing you have a method to read it in your App\Jobs\SendReport
class.