Lance
Lance

Reputation: 3932

Parsing JSON elements in PHP

I have this bit of php that retrieves a JSON string:

$login = "http://$server/auth/login/$username/$password";
$json = file_get_contents($login);

The JSON received is this:

{
   "Return":{
      "Type":"auth.login",
      "ResponseSummary":{
         "StatusCode":0,
         "ErrorMessage":""
      },
      "Results":{
         "SessionID":"fejqciel23dcjqvsumjdplq6s6",
         "User":{
            "Id":"*****",
            "AccountId":"*****",
            "FirstName":"*****",
            "LastName":"******",
            "Email":"*****@*****.***",
            "UserName":"*****@*****.***",
            "Group":"3",
            "Language":"English",
            "Measurement":"Imperial",
            "Timezone":"Etc\/GMT+8",
            "MsgFlag":"0",
            "Pincode":"",
            "Phone":"",
            "TerminationDate":null,
            "DaylightSavings":"0",
            "AcceptedWebsiteHardwareTc":"0",
            "APIContractAccepted":null,
            "DisplayNotice":"1",
            "NoticeInterval":"1 day",
            "TemporaryPassword":false,
            "Roles":[3, 11],
            "UserCode":******,
            "AccountName":"*****@*****.***",
            "CustomAttributes":[]
         },
         "AccountLastChangeDate":"2015-05-27 06:13:28"
      }
   }
}

I am trying to parse out the SessionID. It's the only bit I need. I've followed several other articles here on SO but either get nothing or errors.

A couple of things I've tried:

$jsondata = json_decode($json, true);
echo $jsondata['Return'][0]['Results'][0]['User'][0]['SessionID'];

and

$jsondata = json_decode($json);
echo $jsondata->Return[0]->Results[0]->User[0]->SessionID;

and numerous variations on these. I either get nothing at all or various errors.

What's the correct way to extract the SessionID?

I've hacked it to work using a couple string.explode statements but I'd prefer to do it correctly.

Thanks!

Upvotes: 0

Views: 29

Answers (2)

Byte Lab
Byte Lab

Reputation: 1626

This is the kind of situation where it helps to use php --interactive just to see the variables you're working with:

php > $jsonArr = json_decode($json, true);
php > var_dump($jsonArr);
array(1) {
  ["Return"]=>
  array(3) {
    ["Type"]=>
    string(10) "auth.login"
    ["ResponseSummary"]=>
    array(2) {
      ["StatusCode"]=>
      int(0)
      ["ErrorMessage"]=>
      string(0) ""
    }
    ["Results"]=>
    array(3) {
      ["SessionID"]=>
      string(26) "fejqciel23dcjqvsumjdplq6s6"
      ["User"]=>
      array(24) {
        ["Id"]=>
        string(5) "*****"
        ["AccountId"]=>
        string(5) "*****"
        ["FirstName"]=>
        string(5) "*****"
        ["LastName"]=>
        string(6) "******"
        ["Email"]=>
        string(15) "*****@*****.***"
        ["UserName"]=>
        string(15) "*****@*****.***"
        ["Group"]=>
        string(1) "3"
        ["Language"]=>
        string(7) "English"
        ["Measurement"]=>
        string(8) "Imperial"
        ["Timezone"]=>
        string(9) "Etc/GMT+8"
        ["MsgFlag"]=>
        string(1) "0"
        ["Pincode"]=>
        string(0) ""
        ["Phone"]=>
        string(0) ""
        ["TerminationDate"]=>
        NULL
        ["DaylightSavings"]=>
        string(1) "0"
        ["AcceptedWebsiteHardwareTc"]=>
        string(1) "0"
        ["APIContractAccepted"]=>
        NULL
        ["DisplayNotice"]=>
        string(1) "1"
        ["NoticeInterval"]=>
        string(5) "1 day"
        ["TemporaryPassword"]=>
        bool(false)
        ["Roles"]=>
        array(2) {
          [0]=>
          int(3)
          [1]=>
          int(11)
        }
        ["UserCode"]=>
        string(9) "something"
        ["AccountName"]=>
        string(4) "else"
        ["CustomAttributes"]=>
        array(0) {
        }
      }
      ["AccountLastChangeDate"]=>
      string(19) "2015-05-27 06:13:28"
    }
  }
}

As you can see, $jsonArr['Return']['Results']['SessionID'] will get you what you need. You incorrectly assumed that the elements of your JSON array were 0 indexed.

Upvotes: 2

YvesLeBorg
YvesLeBorg

Reputation: 9089

decode as stdObject

$jsondata = json_decode($json, false);
echo $jsondata->Return->Results->SessionID;

should yield what you are looking for.

Upvotes: 1

Related Questions