0

I have this JSON object:

 $json = '{
   "droplet":{
      "id":3286068,
      "name":"some.domain.com",
      "memory":512,
      "vcpus":1,
      "disk":20,
      "locked":false,
      "status":"active",
      "kernel":{
         "id":377,
         "name":"CentOS 6.5 x64 vmlinuz-2.6.32-431.1.2.0.1.el6.x86_64",
         "version":"2.6.32-431.1.2.0.1.el6.x86_64"
      },
      "created_at":"2014-11-25T19:54:41Z",
      "features":[
         "virtio"
      ],
      "backup_ids":[

      ],
      "next_backup_window":null,
      "snapshot_ids":[
         8724513
      ],
      "image":{
         "id":8385856,
         "name":"rr1.2",
         "distribution":"CentOS",
         "slug":null,
         "public":false,
         "regions":[
            "nyc2",
            "nyc2"
         ],
         "created_at":"2014-12-25T19:09:02Z",
         "min_disk_size":20
      },
      "size_slug":"512mb",
      "networks":{
         "v4":[
            {
               "ip_address":"104.131.211.202",
               "netmask":"255.255.224.0",
               "gateway":"104.131.192.1",
               "type":"public"
            }
         ],
         "v6":[

         ]
      },
      "region":{
         "name":"New York 2",
         "slug":"nyc2",
         "sizes":[

         ],
         "features":[
            "virtio",
            "private_networking",
            "backups"
         ],
         "available":null
      }
   }
}'; 

I can run this:

$arr = json_decode($json);

echo "{$arr->droplet->id}";

and echo out the id just fine. But I have been trying 2 hours to echo the ip_address that is deeper in the JSON object...

How do I echo the ip_address and nothing but the ip_address?

P.S. Please don't show me how to do this with a regex. I can do it that way. I want to learn how to echo "ip_address" in the same way I'm echo'ing the "id".

3
  • 1
    var_dump($arr) to see what the structure is. probably it's not decoding to an object, just a plain php array. Commented Feb 12, 2015 at 20:22
  • 1
    Giving that massive JSON in one line is not particularly helpful for people trying to read the structure. Commented Feb 12, 2015 at 20:25
  • tools like jsonformatter.curiousconcept.com can help Commented Feb 12, 2015 at 20:37

1 Answer 1

2

You just need to look deeper into the JSON object. If you do this:

echo $arr->droplet->networks->v4[0]->ip_address;

It outputs:

104.131.211.202

As far as explaining the breakdown of JSON, consider that each "{" or "[" takes you deeper into the structure, while each "," is horizontal on the current level. This means that id is parallel with networks, and what we want is beneath networks, so we point into it instead. Following that same pattern inside of networks, we go down into v4, which is an array. Positions within an array are referenced through square brackets containing the index. Since we want the first position, we use [0]. Finally, ip_address is on our current level, so we can point to it here.

Sign up to request clarification or add additional context in comments.

1 Comment

I was missing the "[0]" part of "v4[0]" completely. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.