0

I am trying to get values ​​from a nested JSON, which contains keys with spaces. I am having this error:

  • Notice: Trying to get property 'Submit Date' of non-object in json.php on line 47

  • Notice: Trying to get property 'Entry ID' of non-object in json.php on line 46

        {
             "entries": [
                 {
                     "values": {
                                "Entry ID": "INC000000001",
                                 "Submitter": "Remedy Application Service",
                                 "Assigned Group": "TI WIN",
                                 "Priority": "Medium",
                                 "Submit Date": "2022-07-20T22:27:01.000+0000",
                                 "Assignee": "Example asignee"
                                 },
                      "_links": {
                                }
                  }
                         ]
    
          }
    

My PHP Code

 <?php
    $response = curl_exec($curl);
    $data = json_decode($response);
    foreach($data->entries as $entr)
    {
      
        foreach($entr->values as $valores)
         {
    
           $entry_id=$valores->{'Entry ID'};
           $submit_date=$valores->{'Submit Date'};
          }
    }
   ?>

print_r output:

Array
(
    [entries] => Array
        (
            [0] => Array
                (
                    [values] => Array
                        (
                            [Entry ID] => INC000000001
                            [Submitter] => Remedy Application Service
                            [Assigned Group] => TI WIN
                            [Priority] => Medium
                            [Submit Date] => 2022-07-20T22:27:01.000+0000
                            [Assignee] => Example asignee
                         )
                 )
        )
)

Where is the error?

any method to get the values?

0

1 Answer 1

2

Your code is inconsistent with your print_r output; in your code $data will be an object (because you have not specified the $associative parameter to json_decode) which looks like this:

stdClass Object
(
    [entries] => Array
        (
            [0] => stdClass Object
                (
                    [values] => stdClass Object
                        (
                            [Entry ID] => INC000000001
                            [Submitter] => Remedy Application Service
                            [Assigned Group] => TI WIN
                            [Priority] => Medium
                            [Submit Date] => 2022-07-20T22:27:01.000+0000
                            [Assignee] => Example asignee
                        )
                    [_links] => stdClass Object
                        (
                        )
                )
        )
)

The values property of each entry in the entries array is an object, not an array, so you can't iterate it. Instead, just access the properties directly:

foreach($data->entries as $entr) {
    $entry_id=$entr->values->{'Entry ID'};
    $submit_date=$entr->values->{'Submit Date'};
    var_dump($entry_id, $submit_date);
}

Output (for your sample data):

string(12) "INC000000001"
string(28) "2022-07-20T22:27:01.000+0000"

Demo on 3v4l.org

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.