0

I am querying a 3rd party service which outputs json.

$data = json_decode($result,true);
var_dump ($data);

$data holds this:

{"response":{"status":"OK","token":"hbapi:187089:586e655ed1f9c:nym2","dbg_info":{"instance":"53.bm-hbapi.prod.ams1","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"64.bm-hbapi.prod.nym2","slave_hit":false,"db":"master","parent_dbg_info":{"instance":"45.bm-api.prod.nym2","slave_hit":false,"db":"master","time":664.07299041748,"version":"1.17","warnings":[],"slave_lag":0,"start_microtime":1483629917.8571},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"39e1c17a-7fe4-58ac-9486-c4dd5bbf96a3","warnings":[],"time":1159.7349643707,"start_microtime":1483629917.7835,"version":"1.17.150","slave_lag":0,"output_term":"not_found"},"awesomesauce_cache_used":false,"count_cache_used":false,"uuid":"286ca4bc-6964-50ad-b241-ff9df1304278","warnings":[],"time":1347.2578525543,"start_microtime":1483629917.6534,"version":"1.17.150","slave_lag":0,"output_term":"not_found","master_instance":"64.bm-hbapi.prod.nym2","proxy":true,"master_time":1159.7349643707}}}

I am trying to get the token value.

I tried

$token = $data["response"][0]["token"];

I get NULL

I also tried

$token = $data['response'][0]['token'];

And I still get NULL.

I have looked at How can I access an array/object? and other threads - can't find the issue.

Community
  • 1
  • 1
Cody Raspien
  • 1,753
  • 5
  • 26
  • 51
  • Possible duplicate of [Access json object in php](http://stackoverflow.com/questions/15440956/access-json-object-in-php) – Ahad Jan 05 '17 at 15:33
  • you say you do `var_dump($data)` and that json is your output?? – yivi Jan 05 '17 at 15:37

5 Answers5

1
$token = $data["response"]["token"];

json:

{
    "response": {
        "status": "OK",
        "token": "hbapi:187089:586e655ed1f9c:nym2",
        "dbg_info": {

        }
    }
}
harry
  • 483
  • 2
  • 12
0

$data is an object (not an array), so you can access response as a property of this object: $data->response.

response is again an object. etc..

For example status can be called like this: $data->response->status.

You can see what is an object and what is an array using this code:

print "<pre>";
var_dump($data);

So in your case, to get token:

$token = $data->response->token
Graftak
  • 691
  • 7
  • 16
  • Sorry, I overlooked the `true` flag in your `json_decode` call. Well.. if you remove that flag (`$data = json_decode($result);`), this example should work :) Otherwise @Dobe Lee's answer is correct. – Graftak Jan 05 '17 at 16:06
0

Why are you accessing the pos [0] if isn't an array?

Instead of that try to access to the desired parameter like this:

$token = $data["response"]["token"];
avilac
  • 792
  • 1
  • 8
  • 23
0

change $data['response'][0]['token']; to $data['response']['token'];

$data['response'] is, it doesnot have the 0 index.

{
    "status": "OK",
    "token": "hbapi:187089:586e655ed1f9c:nym2",
    "dbg_info": {

}
LF00
  • 27,015
  • 29
  • 156
  • 295
-1

In $data you still have json, so you should call json_decode($data) one more time.

nowaja
  • 79
  • 4