Ankit Dhawan
Ankit Dhawan

Reputation: 51

parsing json data using request function in python.... i am unable to access the objects

My data is similar to this:

{
  "id": "694543830607034",
  "name": "Ankit Dhawan",
  "accounts": {
      "data": [
         {
            "access_token": "a",
            "category_list": [
               {
                  "id": "192119584190796",
                  "name": "Event"
               }
            ],
            "name": "Ignitron 2014", // I want to print this.
            "id": "731808386867764",
            "perms": [
               "ADMINISTER",
               "EDIT_PROFILE",
               "CREATE_CONTENT",
               "MODERATE_CONTENT",
               "CREATE_ADS",
               "BASIC_ADMIN"
            ]
         },

I also want to access the page name (GITM-IEEE,Ignitron14), which I need to access the page name from the JSON.

The code I'm using to try and print the names of the pages:

import requests 
import json
base_url = 'https://graph.facebook.com/me'
ACCESS_TOKEN="XXXXX"
fields = 'id,name,accounts'
url = '%s?fields=%s&access_token=%s' % \
(base_url, fields, ACCESS_TOKEN,)
print url
dat = requests.get(url).json()
for a in dat:
    for b in a["data"]:
        print b["name"]

Upvotes: 3

Views: 112

Answers (2)

Ankit Dhawan
Ankit Dhawan

Reputation: 51

thanks the problem is solved: i use

import requests # pip install requests
import json
base_url = 'https://graph.facebook.com/me'
ACCESS_TOKEN="XXXXXX"
fields = 'id,name,accounts'
url = '%s?fields=%s&access_token=%s' % \
    (base_url, fields, ACCESS_TOKEN,)
dat = requests.get(url).json()
for a in dat['accounts']['data']:
    print a['name']

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1123420

You have your loops mixed up. Your outer-most object is a dictionary containing keys, and those keys reference further objects. Only the data key in the dictionary referenced by the outermost accounts key references a list. Looping over a dictionary yields just the keys (strings), but you can just directly address those. Use the for loop for the 'data' list nested under the accounts key:

for entry in dat['accounts']['data']:
    print entry['name']

You can clean up the requests code a little; use the API to handle GET parameters:

base_url = 'https://graph.facebook.com/me'
ACCESS_TOKEN="XXXXX"
params = {
    'fields': 'id,name,accounts',
    'access_token': ACCESS_TOKEN,
}
dat = requests.get(url, params=parames).json()
for entry in dat['accounts']['data']:
    print entry['name']

Demo with your sample JSON (fixed up to close the open objects):

>>> import json
>>> sample = '''\
... {
...     "id": "694543830607034",
...     "name": "Ankit Dhawan",
...     "accounts": {
...         "data": [
...             {
...                 "access_token": "a",
...                 "category_list": [
...                     {
...                         "id": "192119584190796",
...                         "name": "Event"
...                     }
...                 ],
...                 "name": "Ignitron 2014",
...                 "id": "731808386867764",
...                 "perms": [
...                     "ADMINISTER",
...                     "EDIT_PROFILE",
...                     "CREATE_CONTENT",
...                     "MODERATE_CONTENT",
...                     "CREATE_ADS",
...                     "BASIC_ADMIN"
...                 ]
...             }
...         ]
...     }
... }
... '''
>>> dat = json.loads(sample)
>>> for entry in dat['accounts']['data']:
...     print entry['name']
... 
Ignitron 2014

Upvotes: 3

Related Questions