0

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:

enter image description here

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.

Freddy Daniel
  • 349
  • 2
  • 12
  • Why bother to serialize any data inside of a json encoded string? XY Problem. – mickmackusa Jul 18 '21 at 01:43
  • This was reposability of laravel framework, I just register a job .. I created a job class SendReport and I'm using it to save the table name.. but this encoded laravel did it, here is my class https://pastebin.com/0XfLN8TK I registered a new job using this: \App\Jobs\SendReport::dispatch("employees"); using a laravel route – Freddy Daniel Jul 18 '21 at 01:46
  • @mickmackusa here is the jobs table https://imgur.com/a/DAGwx2E – Freddy Daniel Jul 18 '21 at 01:51
  • `it does not return any value.`, then we Need Debugging Details. What does jsonlint.com say? When I try your data, it is valid json. When I try `$string` it is invalid. https://stackoverflow.com/q/2348152/2943403 – mickmackusa Jul 18 '21 at 05:01

1 Answers1

0

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.

Robo Robok
  • 18,202
  • 14
  • 57
  • 101
  • Hi bro.. not this payload is from php laravel framework. I'm getting this payload from the jobs table.. I'm registering jobs then I need to extrack this payload and make a dump in csv of the table.. another solution is create a new table and only save the table name on there.. but to save space I want get that table name from that job payload. – Freddy Daniel Jul 18 '21 at 00:56
  • @FreddyDaniel I updated my answer. You can do it without WordPress too, I haven't seen anyone using `serialize()` outside of WordPress in a long time. – Robo Robok Jul 18 '21 at 00:59
  • I'm using php 7.4 it no trows nothing.. only says: Notice: Trying to get property 'data' of non-object in C ... Notice: Trying to get property 'command' of non-object in C:\laragon\www.. – Freddy Daniel Jul 18 '21 at 01:02
  • @FreddyDaniel okay, I updated the slashes, as they caused problems with JSON. Please read the end of my answer, as I added something important there. – Robo Robok Jul 18 '21 at 01:11
  • Yes in SendReport class which is a job class from Laravel framework this variables was private here is the class.. https://pastebin.com/0XfLN8TK but I changue to public and the same payload is saved.. it is laravel security? – Freddy Daniel Jul 18 '21 at 01:20
  • @FreddyDaniel I haven't used `serialize()` too extensively. The payload doesn't matter that much, it still uses the real class, so the property's visibility will match what you have defined in the class. The `$tableName` is probably `private` only to have the setter and getter for it. – Robo Robok Jul 18 '21 at 01:30
  • The problem is that I'm not using laravel to read the payload I'm using and external script with make a query the database and access to jobs table. Here is my script https://pastebin.com/iTH4My5q see 34 line code. it don't work. Maybe with laravel 5 I'm using laravel 8 the payload is different – Freddy Daniel Jul 18 '21 at 01:38
  • If you're using serialized data, you should have the referenced classes available. Either load the `App\Jobs\SendReport` or create a mock of it. – Robo Robok Jul 18 '21 at 01:40