0

I have this piece of JSON and I need the FACETS.

I have managed to get the "nbHits" but I am struggling with getting the "facets"

$res = "the json below"

$res = json_encode($res);

$obj = json_decode($res);

print $obj->{'nbHits'}; /// 1116

I have tried

print $obj['facets']

and

print $obj['facets'][0]

I am missing something simple, any help is appreciated.

The JSON

{
  "hits": [{
    "contactId": 986838846,
    "email": "sdfghjhgfdfgh",
    "numOpens": 0,
    "numPageViews": 0,
    "numClicks": 0,
    "numForwards": 0,
    "numEstimatedForwards": 0,
    "numReplies": 0,
    "dateSent": "2016-09-23T13:56:54.52",
    "dateFirstOpened": "1970-01-01T00:00:00",
    "dateLastOpened": "1970-01-01T00:00:00",
    "firstOpenIp": "",
    "unsubscribed": "false",
    "softBounced": "false",
    "hardBounced": "false",
    "dept": "nil",
    "appt": "no",
    "hasClicked": "no",
    "hasOpened": "no",
    "hasReplied": "no",
    "kId": "nil",
    "salesExec": "Charlie",
    "sold": "no",
    "calledCust": "no",
    "objectID": "986838846",
    "_highlightResult": {
     "email": {
       "value": "dfghgfdghj",
       "matchLevel": "none",
        "matchedWords": []
      },
  "dept": {
    "value": "nil",
    "matchLevel": "none",
    "matchedWords": []
  },
  "appt": {
    "value": "no",
    "matchLevel": "none",
    "matchedWords": []
  },
  "hasClicked": {
    "value": "no",
    "matchLevel": "none",
    "matchedWords": []
  },
  "hasOpened": {
    "value": "no",
    "matchLevel": "none",
    "matchedWords": []
  },
  "hasReplied": {
    "value": "no",
    "matchLevel": "none",
    "matchedWords": []
  },
  "salesExec": {
    "value": "Charlie",
    "matchLevel": "none",
    "matchedWords": []
  },
  "sold": {
    "value": "no",
    "matchLevel": "none",
    "matchedWords": []
  },
     "calledCust": {
    "value": "no",
    "matchLevel": "none",
    "matchedWords": []
  }
}
  }],
  "nbHits": 1116,
  "page": 0,
  "nbPages": 1000,
  "hitsPerPage": 1,
  "processingTimeMS": 1,
  "facets": {
   "appt": {
     "no": 994
    },
    "dept": {
  "nil": 994
    },
    "sold": {
  "no": 994
   },
"hasOpened": {
  "no": 629,
  "yes": 365
},
"salesExec": {
  "nil": 394,
  "Alfa": 100,
  "Bravo": 100,
  "Charlie": 100,
  "Delta": 100,
  "Echo": 100,
  "Foxtrot": 100,
  "Hotel": 100
},
"hasClicked": {
  "no": 975,
  "yes": 19
},
"hasReplied": {
  "no": 989,
  "yes": 5
}
 },
  "exhaustiveFacetsCount": true,
  "query": "",
  "params":     "facets=%5B%22hasOpened%22%2C%22dept%22%2C%22appt%22%2C%22sold%22%2C%22salesExec%22%2C%22hasClicked%22%2C%22hasReplied%22%5D&hitsPerPage=1&query="
}
Jason
  • 931
  • 2
  • 12
  • 26

1 Answers1

0

The result is already json encoded format so why you're again encoding it?

To grab the no value on facets try this way $obj->facets->appt->no

$res = 'Your JSON goes here';
//-----Don't do again json_encode() again here
$obj = json_decode($res); 
print '<pre>';
print_r($obj);
print '</pre>';
echo $obj->facets->appt->no;

OR Make life easier by decoding it as an array

$array = json_decode($res,1); //see second parameter here
print '<pre>';
print_r($array);
print '</pre>';
echo $array['facets']['appt']['no'];
echo $array['facets']['hasOpened']['yes'];
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103