json_decode() in PHP not showing true or false statements - php

I have a response coming back that is JSON encoded, but when I decode it I lose the true/false attributes after using $var = json_decode($response);.
Here’s an example:
{
"domain": "my.domain.com",
"created_at": "2014-11-15 00:26:53.74059",
"valid_mx": true
}
I’ve even tried:
$var = json_decode($response, true);
But it still seems to drop the true/false. How can I properly pull the true/false from the response? What am I missing?

Your problem is with print_r, not json_decode.
print_r does not show true / false for true / false. Instead, it shows 1 / (blank).
You can use var_dump($var); or var_export($var); instead which will show you the correct values.

This should work for you:
(With this you have the JSON string as an array)
<?php
$response = '{
"domain": "my.domain.com",
"created_at": "2014-11-15 00:26:53.74059",
"valid_mx": true
}';
$var = json_decode($response, true);
if($var["valid_mx"] === TRUE)
echo "true";
else
echo "false";
?>
Output:
yes
If you want an object just change this line:
$var = json_decode($response, true);
to this:
$var = json_decode($response);
And then you can access it with this line:
if($var->valid_mx === TRUE)

This works for me:
if(json_decode($response)->valid_max){
//your stuff
}

Related

json_decode returns null and bool false

I have code:
<?php $json = '[{"title":" \\ud83c\\uddf7\\ud83c\\uddfa \\u0420\\u043e\\u0441\\u0441\\u0438\\u0439\\u0441\\u043a\\u043e\\u0435 \\u043a\\u0430\\u0437\\u0438\\u043d\\u043e #1
\\ud83c\\uddf7\\ud83c\\uddfa ","desc":"\\u0422\\u043e\\u043b\\u044c\\u043a\\u043e
\\u0437\\u0430\\u0440\\u0435\\u0433\\u0438\\u0441\\u0442\\u0440\\u0438\\u0440\\u0443\\u0439\\u0441\\u044f \\u0438 \\u0438\\u0433\\u0440\\u0430\\u0439
\\u043f\\u0435\\u0440\\u0432\\u044b\\u0439 \\u0440\\u0430\\u0437 \\u0437\\u0430 \\u043d\\u0430\\u0448
\\u0441\\u0447\\u0451\\u0442","icn":"https://1.mbvnclick1.com/ic?sid=2&data=0eikgji0Ck2EKXJkLTJfLie%2FKy%2FvWYZiVPrhxIOQsl6VkyioGiy%2B4DYdpqaaMXlM5dVPkQoRzngoPAlvQ3w1pREOxlMjuR7DQHq6Yz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price":"0.0055"}]';
$json = json_decode($json, true); var_dump($json);
echo '<br>';
echo json_last_error();
?>
It returns null and if I check JSON for errors it returns bool(false). What is wrong? Online decoders are decoding json normally. JSON is advertisment I'm using for test.
I've been searching for issue for hours. I've deleted urls from json, checking if another jsons are working on site (they work). IDK what to do
$json = preg_replace('/[[:cntrl:]]/', '', $json); $array = json_decode($json, true);
I should preg_replace before decoding JSON

Can't print data obtained from parsing JSON

I created this JSON data:
{
"status" : "fail",
"data" : {
"error_message" : "Label already exists on your account for Network."
}
}
The code in my PHP page is:
$newaddr = json_decode($json, true);
echo $newaddr->status;
echo $newaddr->data;
and the result I get when I run it is:
fail
How can I show the value of error_message in my page? Because:
$newaddr->data
has the value null when I print it using var_dump($newaddr->data);
When you use json_decode(..., true); the second parameter means that you want to json to be parsed as associative arrays. But, you are trying to get the data by object property.
The solutions are:
Change the second parameter to false
$newaddr = json_decode($json, false);
echo $newaddr->status;
echo $newaddr->data->error_message;
Get data with array-way
$newaddr = json_decode($json, true);
echo $newaddr['status'];
echo $newaddr['data']['error_message'];
json_decode documentation

How to get value success and value true recaptcha and place to if condition?

this is the code,
$response=#file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
and this the result when I print_r($response);
{ "success": true, "challenge_ts": "2016-09-17T03:11:53Z", "hostname": "www.blabla.com" }
actually I want to make codition with if condition.
for example :
if ($response success == true)
how do that?
The Response you are getting is in JSON format so for that you have to decode to php array and after that you can use it, see below:
$response=#file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$data = json_decode($response, true);
if($data['success'] == true) {
// Here your code
}

Json Parse issue with error Trying to get property of non-object

I want to parse following json for value of
JSON (saved in my.json):
{
"mackoniv":{"entry":"","lastExit":"","userId":"OPENGOVTJOBS","userNick":"mack"},
"johanna":{"entry":"","lastExit":"","userId":"FREEJOBALERT","userNick":"jone"}
}
Code used :
$json = file_get_contents('my.json');
$json_data = json_decode($json,true);
$userToCheck='johanna';
echo 'userId'.$json_data->$userToCheck->userId;
The above code gives error "Trying to get property of non-object", which i understand as $userToCheck isnt an object of $json_data but how do i access the data of "mackoniv" or even "johanna" when userToCheck part is not to be hardcoded.
If i try following way, it gives same error.
echo 'userId'.$json_data[$userToCheck]['userId'];
first of all json_decode($jsondata, true); would return an array. try to var_dump your $json to make sure you've got the file and then var_dump $json_data.
If you write it like below it should work.
$json = file_get_contents('my.json');
$json_data = json_decode($json, true);
$userToCheck = 'johanna';
echo 'userId = ' . $json_data[$userToCheck]['userId'];
Basically change the line:
echo 'userId' . $json_data->$userToCheck->userId;
with:
echo 'userId = ' . $json_data[$userToCheck]['userId'];
if json_decode second paramether is true, it returns an array, if it is false (default), returns an object. So, if you want to use it as an object, just decode it like: $json_data = json_decode($json);. If this doesn't work, check what echo json_last_error_msg(); returns. If it returns "No error", then the problem is that your file_get_contents() is not refering to the correct file.

Pulling data from weird JSON array response

I am working with an API and the response comes back weird. I need to grab the ID field from the response below.
{
"message_campaigns": [
{
"embedded_errors": [],
"id": "2729",
"is_legacy_message": false,
"is_setup_complete": false,
"message_campaign_type_id": 1,
"name": "Message Campaign 1",
"organization_id": 123
}
]
}
Below is my attempt:
$result = curl_exec($ch);
curl_close($ch); // Seems like good practice
$responseData = json_decode($result, TRUE);
print_r($responseData);
I have tried several methods and cannot get it to work
$responseData[0]['id'];
$responseData->id;
I can't figure out how to get the id to display.
Assuming your curl_exec actually returns a response because you've set curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
If I look at your json response you need:
$responseData['message_campaigns'][0]['id'];
It's because the result is an array, as you pass the second parameter as true to json_decode:
When TRUE, returned objects will be converted into associative arrays.
In JSON, {} stands for objects, and [] stands for array. You've converted all objects to associative arrays, so this'll work:
$responseData['message_campaigns'][0]['id']
You have the second parameter of json_decode set to true, so an array (not an object) will be returned.
You'll also need to access message_campaigns as the first item in the array.
$responseData['message_campaigns'][0]['id'];
This can be tested:
$result = <<< EOT
{
"message_campaigns": [
{
"embedded_errors": [],
"id": "2729",
"is_legacy_message": false,
"is_setup_complete": false,
"message_campaign_type_id": 1,
"name": "Message Campaign 1",
"organization_id": 123
}
]
}
EOT;
$responseData = json_decode($result, TRUE);
echo $responseData['message_campaigns'][0]['id'];
// 2729

Categories