Parsing Wordpress Json Output - php

I want to parse this I tried almost everything but I couldn't understand this , I m getting this from WordPress database table wp_postmeta.
Tried - unserialize , serialize, json_encode
string(303) "s:294:"a:1:{i:2364;a:1:{s:9:"schedules";a:1:{s:13:"578db85b83681";a:5:{s:13:"minutes_start";s:3:"540";s:11:"minutes_end";s:4:"1020";s:5:"weeks";a:6:{i:0;s:2:"on";i:1;s:2:"on";i:2;s:2:"on";i:3;s:2:"on";i:4;s:2:"on";i:6;s:2:"on";}s:10:"date_start";s:10:"07/18/2016";s:8:"date_end";s:10:"09/30/2016";}}}}";"

Try unserialize(). This worked.

Related

PHP Json Malformed When Posted From jQuery Ajax

I am having a weird issue, when i use json_decode() on some json from a jquery ajax call it's coming back always saying that the json is malformed (JSON_ERROR_SYNTAX).
I say this is weird because if i take a copy of the raw posted json from the developer console and manually push it through json_decode() then it decodes perfectly fine.
I have uploaded a txt file of the example json here : https://drive.google.com/file/d/1IZ5RkpFK7KLUNYeZe4dPdxGWZXinmFSJ/view?usp=sharing which works manually parsing it but not from posted data. Another weird issue is if i save the json string to a longtext field in a mysql database and then pull it out again, it then decodes fine; but this isn't ideal, it needs to validate it before going to the database and i'm unsure why this would allow it to decode anyway.
Any ideas?
This might be happeing due to new line code added in your code. You can add below code and try to decode the code again it might work.
$content = preg_replace('/[\r\n\t\s]+/s', ' ', $content);#new lines, multiple spaces/tabs/newlines
$content = preg_replace('#/\*.*?\*/#', '', $content);#comments
$content = preg_replace('/^\s+/', '', $content);#spaces on the begining

json_decode in php retruns null

i am trying to parse json its working in online website but not working on json_decode() php, below is my json.
{"category":"group_channel:message_send","sender":{"nickname":"Four Six","user_id":"917777770046","profile_url":"https:\/\/sendbird.com\/main\/img\/profiles\/profile_05_512px.png","metadata":{}},"custom_type":"application\/octet-stream","mention_type":"users","mentioned_users":[],"app_id":"C14BC2EA-ED60-4A82-9441-5C312A6EA991","members":[{"is_blocking_sender":false,"unread_message_count":1,"total_unread_message_count":1,"is_active":true,"is_online":true,"is_hidden":0,"channel_mention_count":0,"nickname":"Four Six","is_blocked_by_sender":false,"user_id":"917777770046","channel_unread_message_count":0,"state":"joined","push_enabled":true,"push_trigger_option":true,"profile_url":"https:\/\/sendbird.com\/main\/img\/profiles\/profile_05_512px.png","metadata":{}}],"type":"MESG","payload":{"custom_type":"application\/octet-stream","created_at":1570690168039,"translations":{},"message":"H4sIAAAAAAAAALWTXYucMBSG\/4rkWockRh3nrrNlYViYFpbSyyUmx5mwmkiMpXaZ\/94T54MpC6Ut\nVASPb3Ke85W8kR7GUR5gp8mGCOCUFm2V6aIpMqHKOpOqEVnRasYbJbhqZFbqNeNFVWdlK1QmqoZm\nUqO+VrpSDLgStCApab3rEVmzankoFSWqYR4A1UvQl9b5HlV1lOGPE8D9g5w7J9HhjSyEy\/eMoAx3\njAEGsmEpAW0CWNl0Mew++nYyPC5ORFrtndEofgM\/GmdRi87KWfQJEYu0FYuGlkHGb+8a08F+6hvw\n78trjR\/DXvYx2KObPEqdvCnP5jsK0EvT4R+aUmuPnUDuKSUHsHphLunLMI1nGyPDp3ZrfDiehcG7\nFnP4bFSYPHzxF1g\/72zroo2w3mnTGtDb+X2S17UPWCErKlrWlDG65hX6xXr5fb0xOiI4ZXXGKL7\/\nSl9f6Pk9\/dYA8pTltUiTbefUa\/KUJs9H+Tr5YfLJg+ucne+UNPkI3dGkCXJpjk47q41cWo0x+XpV\n1oyWeY6CPZBNVa2YKHLBKpysCTHlvcNmJl9hDGfUMvPJBh8Xr7Q4g1j6dccPM8TzscRclj1AeLGX\n0V6Tu2\/PLy2oc178rnOn6Pif7qIZH1w\/dBAgkmfy9xMsebpcsdvBZ5jw6SfcNiZDPgQAAA==\n","data":"message_form","message_id":2839079954},"channel":{"is_distinct":false,"name":"KPGI Workday","custom_type":"kpgi_workday_data","is_ephemeral":false,"channel_url":"sendbird_group_channel_150279781_505f37b8b653c01febe37245d447f7098d0fe53c","is_public":false,"is_super":false,"data":"{\"chatApp\":\"sendbird\",\"chatId\":\"4e2005f7-d5b5-4c69-acb4-5fd12bc42cba\",\"topParentId\":\"4e2005f7-d5b5-4c69-acb4-5fd12bc42cba\",\"formNumber\":\"0\",\"creatorName\":\"Four Six\",\"creatorId\":\"917777770046\",\"topic\":\"KPGI Workday\",\"formstatus\":\"{\\\"kpgi_workday_travel_form\\\":0,\\\"kpgi_workday_initiate_break\\\":0,\\\"kpgi_workday_travel\\\":0,\\\"kpgi_workday_expense\\\":0,\\\"kpgi_workday_close_data\\\":0,\\\"kpgi_workday_timesheet\\\":0}\",\"type\":\"kpgi_workday_data\",\"parentId\":\"\"}","is_discoverable":false},"sdk":"Android"}
Please try this JSON:
{"category":"group_channel:message_send","sender":{"nickname":"Four Six","user_id":"917777770046","profile_url":"https://sendbird.com/main/img/profiles/profile_05_512px.png","metadata":{}},"custom_type":"application/octet-stream","mention_type":"users","mentioned_users":[],"app_id":"C14BC2EA-ED60-4A82-9441-5C312A6EA991","members":[{"is_blocking_sender":false,"unread_message_count":1,"total_unread_message_count":1,"is_active":true,"is_online":true,"is_hidden":0,"channel_mention_count":0,"nickname":"Four Six","is_blocked_by_sender":false,"user_id":"917777770046","channel_unread_message_count":0,"state":"joined","push_enabled":true,"push_trigger_option":true,"profile_url":"https://sendbird.com/main/img/profiles/profile_05_512px.png","metadata":{}}],"type":"MESG","payload":{"custom_type":"application/octet-stream","created_at":1570690168039,"translations":{},"message":"H4sIAAAAAAAAALWTXYucMBSG/4rkWockRh3nrrNlYViYFpbSyyUmx5mwmkiMpXaZ/94T54MpC6Ut\nVASPb3Ke85W8kR7GUR5gp8mGCOCUFm2V6aIpMqHKOpOqEVnRasYbJbhqZFbqNeNFVWdlK1QmqoZm\nUqO+VrpSDLgStCApab3rEVmzankoFSWqYR4A1UvQl9b5HlV1lOGPE8D9g5w7J9HhjSyEy/eMoAx3\njAEGsmEpAW0CWNl0Mew++nYyPC5ORFrtndEofgM/GmdRi87KWfQJEYu0FYuGlkHGb+8a08F+6hvw\n78trjR/DXvYx2KObPEqdvCnP5jsK0EvT4R+aUmuPnUDuKSUHsHphLunLMI1nGyPDp3ZrfDiehcG7\nFnP4bFSYPHzxF1g/72zroo2w3mnTGtDb+X2S17UPWCErKlrWlDG65hX6xXr5fb0xOiI4ZXXGKL7/\nSl9f6Pk9/dYA8pTltUiTbefUa/KUJs9H+Tr5YfLJg+ucne+UNPkI3dGkCXJpjk47q41cWo0x+XpV\n1oyWeY6CPZBNVa2YKHLBKpysCTHlvcNmJl9hDGfUMvPJBh8Xr7Q4g1j6dccPM8TzscRclj1AeLGX\n0V6Tu2/PLy2oc178rnOn6Pif7qIZH1w/dBAgkmfy9xMsebpcsdvBZ5jw6SfcNiZDPgQAAA==\n","data":"message_form","message_id":2839079954},"channel":{"is_distinct":false,"name":"KPGI Workday","custom_type":"kpgi_workday_data","is_ephemeral":false,"channel_url":"sendbird_group_channel_150279781_505f37b8b653c01febe37245d447f7098d0fe53c","is_public":false,"is_super":false,"data":"{\"chatApp\":\"sendbird\",\"chatId\":\"4e2005f7-d5b5-4c69-acb4-5fd12bc42cba\",\"topParentId\":\"4e2005f7-d5b5-4c69-acb4-5fd12bc42cba\",\"formNumber\":\"0\",\"creatorName\":\"Four Six\",\"creatorId\":\"917777770046\",\"topic\":\"KPGI Workday\",\"formstatus\":\"{\"kpgi_workday_travel_form\":0,\"kpgi_workday_initiate_break\":0,\"kpgi_workday_travel\":0,\"kpgi_workday_expense\":0,\"kpgi_workday_close_data\":0,\"kpgi_workday_timesheet\":0}\",\"type\":\"kpgi_workday_data\",\"parentId\":\"\"}","is_discoverable":false},"sdk":"Android"}
I have made some edits. If this works, you can do a string compare to see the differences, but overall, I just deleted some extra slashes.

What kind of data is this, and is it able to change to JSON

I'm working on cargo wordpress plugin, the shipping history page stores the following data in meta_value field in database. Does anyone know what kind of data is this, and how should I use it or change to a JSON.
s:815:"a:4:{i:0;a:7:{s:4:"date";s:10:"2018-03-08";s:4:"time";s:7:"1:00 am";s:8:"location";s:4:"test";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:16:"Shipment Left US";}i:1;a:7:{s:4:"date";s:10:"2018-03-08";s:4:"time";s:7:"1:00 am";s:8:"location";s:4:"test";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:10:"Processing";}i:2;a:7:{s:4:"date";s:10:"2018-09-12";s:4:"time";s:7:"7:27 pm";s:8:"location";s:4:"test";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:10:"In Transit";}i:3;a:7:{s:4:"date";s:10:"2018-09-12";s:4:"time";s:7:"7:31 pm";s:8:"location";s:10:"tofindthis";s:12:"updated-name";s:7:"wpcargo";s:10:"updated-by";i:2;s:7:"remarks";s:1:"1";s:6:"status";s:10:"In Transit";}}";
That looks like a serialized array.
To convert it to JSON you need to deserialize it first and then json_encode it
How to use php serialize() and unserialize()
echo json_encode($arr);
will echo the json enconded string where $arr is the deserialized array.

json_decode() single quote

I want to use json_decode() to decode a string stored in a database like this:
{"results":[{"r":"1","c":"0"},{"r":"2","c":"0"},{"r":"3","c":"0"}]}
The problem is: the function returns NULL.
But when I try the following code, everything works:
$data ='{"results":[{"r":"1","c":"0"},{"r":"2","c":"0"},{"r":"3","c":"0"}]}';
$JO=json_decode($data);
var_dump($JO);
The value returned from the database is exactly the same as I described above.
For all the people who might get the same problem, my problem was that when I was getting data from database to display it, I was adding html style and table parameters to the data, after removing them everything went back to normal.

dump xml string verbatim to mysql db

I need to dump an xml string (coming into a web service as a POST param), directly into a Mysql DB. The column into which I am writing is currently of type 'text', though I have tried blob as well.
Right now, when I save the string and retrieve it later on using sql, it comes back in a serialized like format, like this:
a:1:{s:14:"<?xml_encoding";s:1502:"UTF-8?><record>
<nodes></nodes>
</record>";}
Whereas, I need to parse that xml as a simplexml object, and hence need it to be better formed.
This is what I am doing codewise:
Accept Post Param in web service
Serialize and store it in DB using doctrine ORM, kind of like
$record->xml_rec = serialize($_POST)
Retrieve it and echo it.
Oddly enough, if I unserialize and echo is upon retrial, I get an array. The array looks like this upon print_f
Array
(
[<?xml_encoding] => UTF-8?><record>
<nodes></nodes>
</record>
)
Still not parse-able by simplexml.
Is this a mysql storage problem or am I doing something wrong with the POST params? I've tried htmlspecialchars to save the string in the db, but similar problems remain.
any help is appreciated, thanks
What you have is a "serialized" php array that you can unserialize by using ... PHP's unserialize function.

Categories