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
        )

)
7
  • 3
    Any code sample you tried so far? Commented Jul 12, 2017 at 21:59
  • This is a PHP question, so we assume the data is JSON but accessed in PHP Commented Jul 12, 2017 at 22:02
  • i've tested so many things I've deleted I will retrack. Lee sorry yes it's json decoded Commented Jul 12, 2017 at 22:03
  • i didnt understand what is going on Commented Jul 12, 2017 at 22:03
  • Well, did you try $arr[0]->Error? Commented Jul 12, 2017 at 22:03

4 Answers 4

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

3 Comments

array_key_exists("Error", $data); this returns nothing
is the variable $data referring to (following your example) the first object inside the array (index 0) ? $jsonArray[0] should be returning the stdClass Object
You would need to cast the object to an array.
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
    }
}

Comments

-1
$someobject = json_decode($yourjsongoeshere);

if(isset($someobject["Error"])){
    echo ($someobject["Error"]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.