How can I convert this WordPress JSON entry into PHP array? - json

I am working on an interface to load data from a WordPress application into my own database. One table record comes with this:
a:6:{i:0;s:2:"39";i:1;s:2:"88";i:2;s:2:"89";i:3;s:2:"53";i:4;s:2:"54";i:5;s:2:"91";}
I know what this is representing and I think its a kind of JSON format, but I don't know how to convert this string into a readable PHP array.
I´ve tried to explode() something like explode(';'), but the result doesn't make any sense.
Have anyone seen this and can help me?
Thanks.

That's not a JSON string. It's a serialized array.
To make the serialized string into a regular array again, use unserialize:
$serialized_array = 'a:6:{i:0;s:2:"39";i:1;s:2:"88";i:2;s:2:"89";i:3;s:2:"53";i:4;s:2:"54";i:5;s:2:"91";}';
$unserialized_array = unserialize($serialized_array);
var_dump( $unserialized_array );

Related

JSON in Wordpress DB: two keys/values for a value/key

I'm trying to understand what kind of json this structure can be:
{s:11:"current_tab";s:7:"content";}
At least I believe that it is json, can anybody can help me understanding how I can query and work with this?
I found it in a mysql DB with wordpress, in the wp_postmeta table.
These arrays are called serialized data representation. That type is used for storing or passing PHP values without losing their type or structure, you can normally find this type of data stored in plugins or themes configuration, but it's widely used when working with WordPress databases.
Let’s say a theme is creating an array for storing color and a path.
In pure PHP, it looks like:
$settings = array(
'color' => 'green',
'path' => 'https://example.com'
);
When that array is stored in the database, it is converted into the serialized representation and looks like:
a:2:{s:5:"color";s:5:"green";s:4:"path";s:18:"https://example.com";}
The advantage is that the serialized data representation can be stored in the database much more effectively than the PHP array. The drawback is that the serialized data can not be changed by a simple search & replace as you would do with a text editor.
You can find more info about this type of data (and also for the PHP methods used to create and retrieve them) by searching for serialized data in WordPress. Also you can find a detailed example here

Weird looking "json" code - how can I convert it to php/array/json?

I have this data code and I need the data in PHP array/object format, anyone recognizes this?
It looks like json but its not. I have pulled it from a WordPress database
a:3:{i:0;a:10:{s:17:"availability_type";s:6:"custom";s:9:"from_date";s:16:"2020-05-14 18:00";s:13:"from_week_day";s:1:"1";s:10:"from_month";s:1:"1";s:9:"from_time";s:0:"";s:11:"to_week_day";s:1:"1";s:8:"to_month";s:1:"1";s:7:"to_date";s:16:"2020-05-14 18:00";s:7:"to_time";s:0:"";s:10:"is_bokable";s:3:"yes";}i:1;a:10:{s:17:"availability_type";s:6:"custom";s:9:"from_date";s:11:"2020-05-15 ";s:13:"from_week_day";s:1:"1";s:10:"from_month";s:1:"1";s:9:"from_time";s:0:"";s:11:"to_week_day";s:1:"1";s:8:"to_month";s:1:"1";s:7:"to_date";s:11:"2020-05-15 ";s:7:"to_time";s:0:"";s:10:"is_bokable";s:3:"yes";}i:2;a:10:{s:17:"availability_type";s:6:"custom";s:9:"from_date";s:11:"2020-05-27 ";s:13:"from_week_day";s:1:"1";s:10:"from_month";s:1:"1";s:9:"from_time";s:0:"";s:11:"to_week_day";s:1:"1";s:8:"to_month";s:1:"1";s:7:"to_date";s:11:"2020-05-27 ";s:7:"to_time";s:0:"";s:10:"is_bokable";s:3:"yes";}}
Sorry for my bad english.
This is a serialized string, you can use unserialize() to convert to an array.
https://www.php.net/unserialize

How to convert this (strange formated) data ? a:5:{s:15:"nc_notification";s:1:"2";s:20:"iso_addToBook"

I tried to extract some data from a Contao Database.
I don't know this format. I search a way to convert it to JSON or something.... To make it readable.
Maybe someone know this format?...
a:5:{s:15:"nc_notification";
s:1:"2";
s:20:"iso_addToBook";
s:1:"1";
s:22:"checkout_skippable";
a:1:{i:0;s:16:"shipping_address";}
It looks like five array-elements with a predefined string-length.
Does anyone know this format? How is it called?
That format is php serialized, something is missing in your string.The correct format should be:
$a = ["nc_notification","2","iso_addToBook","1","checkout_skippable",["shipping_address"]];
It returns:
serialize($a) = "a:6:{i:0;s:15:"nc_notification";i:1;s:1:"2";i:2;s:13:"iso_addToBook";i:3;s:1:"1";i:4;s:18:"checkout_skippable";i:5;a:1:{i:0;s:16:"shipping_address";}}"
you can make unserialize to get back your array.

How do I store a JSON String inside of a JSON String?

I'm using jsoncpp to store information in JSON format. I now have the need to store a json string inside of another json string... In other words, I need to store a sub_item inside of an item.
Using jsoncpp to generate the JSON string, I get this...
{"id":"1","name":"Advil","sub_item":"{\"id\":\"2\",\"name\":\"Liquid Gel Advil\"}\n"}
Which works perfectly fine during runtime. However, when my program saves this information into a MySQL database (on exit) and then loads it back up when I restart the program, it loads the same JSON string from the MySQL database, but it now looks like this...
{"id":"1","name":"Advil","sub_item":"{"id":"2","name":"Liquid Gel Advil"}"}
Which is an invalid JSON string. I'm not sure why this is happening can someone please tell me what the heck is going on...
My MySQL query string reads like so:
UPDATE json_string_test SET jsonstring='{"id":"1","name":"Advil","sub_item":"{\"id\":\"2\",\"name\":\"Liquid Gel Advil\"}\n"}';
Upon further research I found out FastWriter was depreciated, and StreamWriterBuilder was the recommended writer. However, it still produced the same problem as FastWrtier....
I managed to rig a fix by doing the following...
1) Before saving to the database, I replaced ALL substrings matching \" to \\" in ONLY the child JSON string (with id 2).
2) Upon loading the JSON string, I replaced ALL substrings matching \\" to \" in ONLY the child JSON string (with id 2).
I don't understand why the heck I have to do this, so if anyone has a better solution or an explanation... I'd love to hear it.
I think you need to transpose your last 2 characters (assuming your posted string is verbatim). You have
Advil\"}\n"}
but I think you need
Advil\"}\n}"

Valid JSON Format?

I have this complex JSON Format. I don't know Whether is this valid json format or not because the key i am looking for is not in the first two dictionaries but is there in the third dictionary ? Is this valid json format ? If so , how to do parsing in this scenario ?
As am not allowed to ask new question . I edited my old question.
At the end of the first two if blocks, if you put a return statement, that will stop the program from going to execute the Alamofire block.