parsing text / json with php - php

{"email":"test#example.com","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"},"category":["EST_TEST","Newsletter"],"event":"open"}
I'm having trouble parsing this string. It's from Sendgrid's Event API, it seems to be "almost" JSON, but json_decode won't work. My goal is to get the data into an array, then into a MySQL table. I'm not asking anyone to write the code for me, just point me toward the correct method. Do I use the explode function, then json_decode?.
(I'm slowly teaching myself PHP, sorry if the question is not clear)

I think you're not using the json_decode function with $assoc = true, so you're getting an object rather than an array
$json =
'{"email":"test#example.com","timestamp":1346345321,"newsletter":{"newsletter_user_list_id":"3648511","newsletter_id":"613267","newsletter_send_id":"657025"},"category":["EST_TEST","Newsletter"],"event":"open"}';
var_dump(json_decode($json, true));
You'll get the result as array;

Related

JSON won't decode from string in PHP

Good evening,
This is probably a stupid question but I've been fiddling with this for a while now. I have a string coming from an AJAX call. To inspect the actual string that gets sent to my PHP from some JavaScript I put it into the result and spit it out in the front end. The string looks like this:
count: "[{\"cartKey\":\"d9d4f495e875a2e075a1a4a6e1b9770f\",\"qty\":\"3\"},
{\"cartKey\":\"67c6a1e7ce56d3d6fa748ab6d9af3fd7\",\"qty\":\"2\"},
{\"cartKey\":\"f7177163c833dff4b38fc8d2872f1ec6\",\"qty\":\"32\"}]"
So! My problem is getting this to be an actual PHP array. If I do this:
$result['count'] = json_decode($updates, true);
inside my PHP, the result is 0.
Ignore the count name on the result. I'm just trying to turn the above string into an array of objects I can use in PHP rather than a JSON string.
Thanks in advance!
I'm a birk.
The actual value of $updates was null, I think.
I have just solved my problem by not calling JSON.stringify on the data sent from the AJAX call.
The value now comes through and is actually decoded as if by magic within the PHP script.
Thanks for taking a look.
Ash

How can i check if shape:[] is null in json with php

I have a json object like this :
{"Techne":{"#Version":"2.2","Author":"ZeuX","Name":"","PreviewImage":"","ProjectName":"","ProjectType":"","Description":"","DateCreated":"","Models":[{"Model":{"GlScale":"1,1,1","Name":"","TextureSize":"1,1","#texture":"texture.png","BaseClass":"ModelBase","Geometry":{"Folder":[],"Shape":[],"Piece":[],"Null":[]}}}]}}
How can I check if the array Shape:[] is empty with PHP I only know how to do it with JavaScript but this matter needs PHP to be used how can I do it?
You can use json_decode() and then access it's members.
$json = json_decode('{"Techne":{"#Version":"2.2","Author":"ZeuX","Name":"","PreviewImage":"","ProjectName":"","ProjectType":"","Description":"","DateCreated":"","Models":[{"Model":{"GlScale":"1,1,1","Name":"","TextureSize":"1,1","#texture":"texture.png","BaseClass":"ModelBase","Geometry":{"Folder":[],"Shape":[],"Piece":[],"Null":[]}}}]}}');
if(empty($json->Techne->Models[0]->Model->Geometry->Shape))
echo "Shape is empty";
json_decode() will give you an object in this case, because that is how this JSON string is formatted. You can learn more by searching about JSON and PHP, there are plenty of tutorials that will help you understand better.
I hope it helped you.

Can PHP easily parse JSON objects into PHP objects?

More or less, my question is as above.
I have A lot of data i am going to serialize and send to the server. With that said, is there a PHP function to parse it into PHP-objects to manipulate on the Serverside?
My thought is yes due to the dynamic nature of PHP, but i wasnt sure what it would be.
You essentially answered your own question. From the PHP manual entry for json_decode:
json_decode
Takes a JSON encoded string and converts it into a PHP variable.
That said, you'll obviously want to do all the requisite error checking and such.
You can also use json_decode with the true parameter to effectively convert it into to a PHP array like so: $var = json_decode($object,true);

Looping through a JSON string

I am trying to loop through a JSON string using a foreach(). However I keep getting the following error:
"Notice: Trying to get property of non-object".
The strange part is that when I copy and paste the JSON string and then run the foreach(), it work fine. Just to provide some detail, I am using the Best-Buy API.
Since this seemed to work fine for everyone here is it possible there is something wrong with the data that best buy is feeding me?
Please help, I have tried everything!
UPDATE sorry for not posting code. here it is:
$info = json_decode($test, true);
function tagGen($info){
foreach($info as $key => $value){
}
As you have not posted the code so we can just think that you have a string jason encoded. My dear json encoded string is some thing which is some sort of javascript form. And foreach is a php loop. So if you have a json encoded string and you want to use it in foreach loop you have to use json_decode function for that.
When you will apply json_decode you will be having a string From the documentation
Returns the value encoded in json in appropriate PHP type.
Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively.
NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
For some example code.
$str=json_decode($yourjson);
foreach($str as $key=>$value)
{}
You are probably not using json_decode on the JSON string. Take a look at how I would do this:
$json = "somejsonstring";
$json_array = json_decode($json, true);
foreach($json_array as $element) {
echo $element['some key'];
}
Note the "true" second parameter given to the json_decode method. That returns an associative array rather than a standard PHP object. Makes it a lot easier to work with in foreach loops.
Hope that helps, although your question should really include a code sample.
Print your json string somewhere you can copy it and then check if it is valid using this tool: http://jsonformatter.curiousconcept.com/

Data stored in json

I have my data stored in a JSON string like these...
a:1:{s:15:"s2member_level1";s:1:"1";}
How can i read this values in mysql?
I need to know if the value "s2member_level1" is 1.
Thanks!!!
That's not JSON but a string resulted from calling serialize() in PHP. You cannot parse it easily in MySQL. If you can use PHP, use the unserialize function:
$obj = unserialize($data_from_mysql);
if ($obj['s2member_level1'] == 1) {
// more code here
}
You can convert data to JSON in PHP using the json_encode function. In a similar way, you construct an object from a JSON string using json_decode.
#Lekensteyn is correct, but you could do a like statement, although its performance would most likely be very poor. My true answer is to change how you store this information to take advantages of best performing queries.
select * from table
where column like '%s:15:"s2member_level1";s:1:"1";%';
#Lekensteyn is right about the type of this particular String, but for others, PHP has json_decode which you can use to convert a JSON object to a PHP object. It would be considerably more difficult to read such an object using MySQL only.
This is no json, but serialized data. It was probably serialized with the 'serialize' function of PHP. Try:
print_r(unserialize('a:1:{s:15:"s2member_level1";s:1:"1";}'));
... to unserialize it.

Categories