Reputation: 121
I am having a problem parsing the JSON results from a facebook graph api query (2.5) in python.
I'm trying to get the comments on a status message.
Here's the direct url: where "specific_post_id" is the id of a status post of a page. where "access_token" is the app_id and secret_key from facebook.
The graph explorer is useful as well to see the actual JSON https://developers.facebook.com/tools/explorer/
def getFacebookPageCommentData(post_id, access_token, num_comments):
# construct the URL string
base = "https://graph.facebook.com"
node = "/" + post_id + "/comments/"
parameters = "?fields=id,message,created_time,like_count,parent&limit=%s&access_token=%s" % (num_comments, access_token) # changed
url = base + node + parameters
# retrieve data
data = json.loads(request_until_succeed(url))
return data
test_comment = getFacebookPageCommentData(post_id, access_token, 1)["data"][0]
print test_comment
[{u'created_time': u'2016-02-23T22:37:34+0000',u'id': u'XXXXXXXXXXXXX_XXXXXXXXXXX',u'like_count': 2,u'message': u'"Your" ? Government'}]
I expected to get some nested JSON back with a "from" field that contains additional data. However, I don't get that back. I'm sure there is some small error, but I can't seem to find it.
Upvotes: 1
Views: 958
Reputation: 3307
The problem is that the .JSON returned is pretty confusing and has a lot of nested levels.
If you replace the construction of the URL inside your function:
base = "https://graph.facebook.com/v2.5/"
parameters = "/comments/?access_token=%s" % access_token
url = base + post_id + parameters
Then in the .JSON string returned there are two data
entries:
getFacebookPageCommentData(post_id, access_token, 1)["data"]
getFacebookPageCommentData(post_id, access_token, 1)["comments"]["data"]
The second of these contains the from
information. To then access the from
data we then do
getFacebookPageCommentData(post_id, access_token, 1)["comments"]["data"][0]["from"]
Upvotes: 1