Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode() returning error "Notice: Trying to get property of non-object"

Tags:

json

php

twitch

I am trying to write a script that gets a JSON file from a remote location (in this case being twitch.tv) using cURL (don't think that part is too relevant, though I better mention it anyway). For example purposes, lets say the JSON object it returns looks something like this after being stored in a variable:

$json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}

I access the "stream" property, I have tried the follow code:

<?php
    $json_object = {"_links":{"self":"https://api.twitch.tv/kraken/streams/gmansoliver","channel":"https://api.twitch.tv/kraken/channels/gmansoliver"},"stream":null}

    $json_decoded = json_decode($json_object, true);
    echo $json_decoded->stream;
?>

When I try this, I get the error "Notice: Trying to get property of non-object in D:\Servers\IIS\Sites\mysite\getstream.php on line 48".

Am I using json_decode() wrong, or is there something wrong with the JSON object I am being sent from twitch?

Edit:

I now have the JSON object:

{"access_token": "qwerty1235","refresh_token": "asdfghjkl=","scope": ["user_read"]}

If I try to decode it using json_decode() I get the following error: Object of class stdClass could not be converted to string. Any advice?

Thanks in advance for any help

like image 821
grahamhoyes Avatar asked Aug 08 '14 01:08

grahamhoyes


People also ask

What json_decode () function will return?

The json_decode() function can return a value encoded in JSON in appropriate PHP type. The values true, false, and null is returned as TRUE, FALSE, and NULL respectively. The NULL is returned if JSON can't be decoded or if the encoded data is deeper than the recursion limit.

Why is json_decode returning null?

If json_decode returns null is it because the database. json is not valid. You can fix this by open the database.

Does json_decode throw error?

json_decode with flags: JSON_THROW_ON_ERROR throws a JsonException "Syntax Error" when called with an empty string #6547.

What is json_decode?

The json_decode() function is used to decode or convert a JSON object to a PHP object.


2 Answers

You're decoding the JSON into an array. json_decode($json_object, true); Will return an array

array (size=2)
  '_links' => 
    array (size=2)
      'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
      'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
  'stream' => null

If you remove the second parameter and run it as json_decode($json_object)

object(stdClass)[1]
  public '_links' => 
    object(stdClass)[2]
      public 'self' => string 'https://api.twitch.tv/kraken/streams/gmansoliver' (length=48)
      public 'channel' => string 'https://api.twitch.tv/kraken/channels/gmansoliver' (length=49)
  public 'stream' => null

See the documentation, When TRUE, returned objects will be converted into associative arrays.

like image 122
Bankzilla Avatar answered Oct 18 '22 21:10

Bankzilla


You have set the second parameter ($assoc) of json_decode() to true, which means it's going to return an associative array instead of an object. You then tried to reference the object style. If you are setting the second parameter to true, you need to use the associative array style to access the stream content. It would be:

$json_decoded['stream']

If you set the $assoc parameter to false (or do not specify the parameter) then you can reference it as an object:

$json_decoded->stream

If you do var_dump on the $json_decoded variable you will see what it looks like. This is a good way to see what you are working with.

like image 4
johnjg12 Avatar answered Oct 18 '22 21:10

johnjg12