1

I have the following php logic that queries a couchbase database:

$myCluster = new CouchbaseCluster('couchbase://localhost');
$bucket = $myCluster->openBucket('reporting');
$result = $bucket->get('server_details_1');
$doc = $result->value;
var_dump($doc);
echo $doc->server_details->name."\n";

It's failing on the last line because I believe the way I'm trying to access the json object is incorrect. Perhaps it's no longer a json object... I'm not too sure. But I do know that couchbase stores it as json in the database itself.

This is what the var_dump command returns which shows you the structure of the object:

string '{"server_details":{"name":"test_server","dns_name":"test.server.mydomain.net","ipv4":"10.1.16.106","ipv6":"","type":"primary"},"server_status":{"up_to_date":true},"packages":{"pack1":{"current_version":"x.x","previous_version":"x.x","last_updated_on":"2016-03-16","play_id":"link to audit table"},"pack2":{"current_version":"x.x","previous_version":"x.x","last_updated_on":"2016-03-16","play_id":"link to audit table"}}}' (length=438)
Happydevdays
  • 1,982
  • 5
  • 31
  • 57
  • Possible duplicate of [Parsing JSON file with PHP](http://stackoverflow.com/questions/4343596/parsing-json-file-with-php) – skrilled Mar 30 '16 at 18:27
  • That's a valid json string but you have to get php to read it as an object to do what you want to do (json_decode) – georaldc Mar 30 '16 at 18:30

1 Answers1

1

You have to json_decode your data first, because when you get it from the server it is just a string.

$doc = json_decode($result->value);
echo $doc->server_details->name."\n";
raidenace
  • 12,789
  • 1
  • 32
  • 35