PHP Decode nested""" JSON

Home - PHP Decode nested""" JSON

Jordan Maulana asked 1 year ago

i did it..
i can read data from my bucket with PHP..

but i really” hard to find any tutorial about decoding such JSON.
because the structure of received JSON is like 

class -> array -> class -> array

the result of PHP var_dump is like below :

object(stdClass)#31 (1) { [“results”]=> array(1) { [0]=> object(stdClass)#29 (4) { [“createdAt”]=> string(24) “2016-11-09T13:37:17.660Z” [“metadata”]=> object(stdClass)#16 (6) { [“do”]=> int(10) [“lat”]=> float(-7.275117) [“long”]=> float(112.79509) [“pH”]=> int(5) [“sal”]=> int(33) [“temp”]=> int(44) } [“objectId”]=> string(10) “7IaLsNOkWy” [“updatedAt”]=> string(24) “2016-12-08T15:20:22.919Z” } } }

many tutorials are just
-array
or
-class -> array
so i am really confused.

please help..

Jordan Maulana replied 1 year ago

object(result) -> array[result] -> object(bucket column) -> array[bucket column] -> object(metadata) -> array[metadata]
3 nested object + array. how can i parse this? please help

5 Answers
Best Answer
Badrullami Staff answered 1 year ago

Hi Jordan,
If you confused about metadata reponse after json_decode. Please add boolean true in the decode process. 
$bodyAuth = (string) $respAuth->getBody();
$auth = json_decode($bodyAuth, true);
print_r($auth);
You will get array format. 
Hope it’s help.

Jordan Maulana answered 1 year ago

Array ( [results] => Array ( [0] => Array ( [createdAt] => 2016-11-09T13:37:17.660Z [metadata] => Array ( [do] => 10 [lat] => -7.275117 [long] => 112.79509 [pH] => 5 [sal] => 33 [temp] => 44 ) [objectId] => 7IaLsNOkWy [updatedAt] => 2016-12-08T15:20:22.919Z ) ) )

the result of your answer is like that..
now what must i do if i want to take the parameters of metadata?

i tried many but the response is always undefined index / undefined offset

Badrullami Staff answered 1 year ago

Hi Jordan,
If you want to get value on metadata, you can loop your result response. Like this :
$auth = json_decode($bodyAuth, true);
foreach($auth[‘results’] as $row){
      print_r($row[‘metadata’]);

Hope it’s help.

Jordan Maulana answered 12 months ago

it works.,!!!!!!
THANKS A LOT LOT LOT LOT

Girly Saputri Staff answered 12 months ago

Hi Jordan,
Are you satisfied with Badrullami’s answer? If yes, then please click the check button to solve this question. 
If you don’t, please let me know what can Mesosfer community do for you.

Jordan Maulana replied 12 months ago

yes i am satisfied..

Jordan Maulana replied 12 months ago

yes i am satisfied..

Jordan Maulana replied 12 months ago

yes i am satisfied..

Your Answer