how to convert json to string - php

how do i convert a json object to a string
i want to insert a json object to a mysql DB

You might be interested in json_encode().
On the other hand if you already got something json encoded then it already is a string and you can simply store it as-is in the database.

JSON itself is a string.
People use json_encode() and json_decode() to convert objects/arrays of PHP to string and back to objects/arrays.

$array = json_decode($json,true);
$string = implode(",",$array);
Now you can save this string in db as text.

Related

JSON_ENDODE why use to pass a PHP array?

I want to convert an array of objects to JSON encoding, but how it is possible to encode PHP array into JSON array.
if you want to convert php array into json
use:
json_encode($array);
json_encode
for decode
json_decode($array);
json_decode

Serilaizing in PHP returns wrong value

I try to serialize my data with PHP. Unfortunaly, the serialize() function returns a wrong value.
String to be serialized:
{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}
What serialize() returns on my server:
serialize($code);
s:204:"{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}";
What should be returned (https://duzun.me/playground/serialize):
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
You need to json_decode it first to get the wanted result:
When you use the boolean switch as second parameter in json_decode it will be an array instead of an object.
$serialized = serialize(json_decode($inputString, true));
echo $serialized;
// output:
// a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
The site you're using isn't clear about what it's doing, but it appears to be treating the string as JSON and decoding to an array before serializing it as PHP. If you want to replicate this, you can use:
$str = '{"2c4cfd9a340dd0dc88b5712c680c1f88":{"type":"product_custom","layout":"default","size":"medium_large","attributes":{"62d7d5184b7a313dc64255bdb8187847":{"type":"image","color":"#FFFFFF","image":"36018"}}}}';
echo serialize(json_decode($str, true));
a:1:{s:32:"2c4cfd9a340dd0dc88b5712c680c1f88";a:4:{s:4:"type";s:14:"product_custom";s:6:"layout";s:7:"default";s:4:"size";s:12:"medium_large";s:10:"attributes";a:1:{s:32:"62d7d5184b7a313dc64255bdb8187847";a:3:{s:4:"type";s:5:"image";s:5:"color";s:7:"#FFFFFF";s:5:"image";s:5:"36018";}}}}
As pointed out in the comments, unless there's a specific reason you need serialized PHP, then just stick with the serialized JSON string you already have - it'll be both more readable and portable.

Best way to json encode an array that has a json encoded value already? PHP

I have an array with multiple items in it one of which is an already encoded json string. I'm wanting to json encode the whole array but in doing so it re-json_encodes the json and adds slashes to it. The only way I've found to fix this is to json_decode the value and then encode the whole array. I feel like this is a waste of resources though and I feel like there has to be a better way. Is doing it that way the best possible way?
Here's a sample of the array I'm trying to json_encode.
$arr = array();
$arr["var1"] = '{"test":"test"}';
$arr["var2"] = 'foo';
$arr["var3"] = 'bar';
If I don't decode the var1 first and I just encode the whole array I get a result like so
{"var1":"{\"test\":\"test\"}","var2":"foo","var3":"bar"}
Notice the slashes in the json object.
json_encode() returns a string containing the json representation of a value.
In the example, a php string is passed as one element of the array '{"test":"test"}', thus json_encode() is encoding it appropriately into json format, with escaped quotes "{\"test\":\"test\"}".
If decoding nested json is a very resource heavy task, a workaround is to use a placeholder instead of the value, {"var1":"PLACEHOLDER","var2":"foo","var3":"bar"}, and then using str_replace() to replace it.
However, simply decoding it as you described is probably a cleaner solution, if its not resource heavy.

How to convert JSON into an array or string?

I'm not sure if I'm allowed to ask these types of questions. Anyway, I have this array value $custom_fields['user_gender'][0] that outputs this:
a:2:{i:0;s:7:"Male";i:1;s:7:"Female";}
I'm not sure how to convert this into an array or string.
The example data looks like it is serialized PHP. Serializing is a way an array, string etc. can be "represented" as a textual string and can later be converted back to the original data format, using a unserialize operation. A serialized string can be stored as text in a database and can be unserialized at any point.
You can unserialize it with:
$array = unserialize($custom_fields['user_gender'][0]);
This should return an array. If you var_dump on $array, you can be sure it's an array.

Parsing twitter stdClass string in PHP

I have this string stored in a database column:
O:8:"stdClass":2:{s:4:"type";s:5:"Point";s:11:"coordinates";a:2:{i:0;d:-23.5663719;i:1;d:-46.65284157;}}
And I just want the "coordinates" in the middle:
-23.5663719,-46.65284157
There is a easy way to parse it in PHP to get this value?
I tried json_decode but it didn't work.
Thanks.
It's not json encoded, but serialized using serialize().
So what you have to do is:
$o = unserialize($str); // where $str is a string fetched from DB
var_dump($o->coordinates);
References:
http://www.php.net/manual/en/function.serialize.php
http://www.php.net/manual/en/function.unserialize.php

Categories