0

I get back JSON which puts multiple objects as values of an array.

I need to detect if the "Error" property inside the JSON comes back, but in the chance the Error property doesn't exist I don't want an error back.

This is the decoded JSON I get with. JSON could come back like this, but it might not:

JSON

[{"Error":"1050"}]

MY PHP

$data = json_decode($json); 
print_r($data); 

which returns:

Array
(
    [0] => stdClass Object
        (
            [Error] => 1050
        )

)
Omar
  • 786
  • 3
  • 13
  • 34

4 Answers4

2

I think for your specific example, you would access the error with $arr[0]->Error but the assoc options in json_decode means that it depends on what you asked json_decode to do.

Which means if you did $arr = json_decode($json, true), then you would access the error with $arr[0]['Error'] because the JSON will then always decode objects into an associative arrays.

This is a lovely example of mutation and confusion with JSON and PHP, it's possible that you start with an associative array in PHP, convert it to JSON and back and lose the associative array. Just something to keep an eye on.

In PHP, you access arrays with [ square brackets ] and object properties with -> the arrow. They're not interchangeable like the bracket and dot notations in JavaScript. So you always need to be mindful of whether your data structures are objects, or associative arrays.

As for testing if the propery exists, you use isset:

if(isset($arr[0]->Error)) {
  // handle error
}

Now, I really hate using isset everywhere, so I have a utility function:

function getProperty($object, $propertyName, $defaultValue = false)
{
    $returnValue = $defaultValue;

    if (!empty($object)) {
        if (is_array($object)) {
            if (isset($object[$propertyName])) {
                $returnValue = $object[$propertyName];
            }
        } else {
            if (isset($object->$propertyName)) {
                $returnValue = $object->$propertyName;
            }
        }
    }

    return $returnValue;
}

Which means in my code, I do:

if(($error = getProperty($arr[0], 'Error')) === false) {
    // process $error
}

... but that was borne out of always wanting uninitialised values be given default values when they didn't exist, most of the time. (The function is so big because it also works on objects and arrays)

Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
  • But this way, in the chance the JSON does NOT return this Error property, I get back Notice: Undefined property: stdClass::$Error - I need to just check if its there but if I manually refer to the property as Error I get an error back if it doesn't exist. – Omar Jul 12 '17 at 22:15
0

You could use json_decode with the variable that contains your returned JSON and then use array_key_exists to check if the object contains the error key.

array_key_exists("error", $jsonArray)

Similar question to How to check if an array element exists? it seems.

Xamthi
  • 34
  • 6
0

Technically you could cast the object to an array and use array_key_exists(). It's not the most elegant solution but it will work.

$json = '[{"Error":"1050"}]';

$j = json_decode($json);


if (array_key_exists(0, $j)) {

    if (array_key_exists('Error', (array)$j[0])) {
        // It exists
    }
}
Kevin P
  • 601
  • 3
  • 9
-1
$someobject = json_decode($yourjsongoeshere);

if(isset($someobject["Error"])){
    echo ($someobject["Error"]);
}
user2102266
  • 539
  • 3
  • 14