This is the JSON array:
{
"id": "",
"name": "",
"posts": {
"data": [
{
"likes": {
"data": [
],
"paging": {
"cursors": {
"before": "QVFIUnpFd2IwMlhuOWI3dJqelFNNEZAPWDlqeENTNkg1N2RqMm9zQXBreVV6bE9KNXJzX29LeXlMZAzhNT2x3TEhlcGk3OG1Bd3ZABRmpyTXhnNDZAWV2hR",
"after": "QVFIUl9Vbm14cld0dm41OTFtKYmgtelBKall2bnBINjBQMXBiNkNCMUM0d21lQXptOXRvbklkU0pHbV9yejNBVG9Jb2hqQTFoem1mdm9zMnJn"
},
"next": ""
},
"summary": {
"total_count": 84,
"can_like": true,
"has_liked": false
}
},
"comments": {
"data": [
{
"created_time": "2022-05-25T18:22:19+0000",
"message": "",
"id": ""
},
{
"created_time": "2022-05-25T18:51:24+0000",
"message": "jfksjdkf",
"id": ""
},
{
"created_time": "2022-05-26T03:36:32+0000",
"message": "ا",
"id": ""
},
{
"created_time": "2022-05-25T23:43:13+0000",
"message": "",
"id": ""
},
{
"created_time": "2022-05-25T20:36:36+0000",
"message": "kjlfkajsdlkafjowiuerjdsdflkjaslkdfjasdkfjoiuerksdjlkfjsadlkfjiasure",
"id": ""
},
{
"created_time": "2022-05-25T18:42:41+0000",
"message": "jsfkasjdfkasjdflkjasdlkfjasd",
"id": ""
},
{
"created_time": "2022-05-25T17:51:12+0000",
"message": "",
"id": ""
}
],
"summary": {
"order": "ranked",
"total_count": 7,
"can_comment": true
}
},
"id": ""
},
{
"likes": {
"data": [
],
"paging": {
"cursors": {
"before": "QVFIUl9aRjNZAcV92c2ZAPU1Y3bUhzVWI5em5uYVdDbnFMYURiMEF0QWxHYmJZAUzNEbkNZAX1o3R3VwbFFKOUFBcnA3dnl6WFZAmVTVn",
"after": "QVFIUnJ3dDJvZAF9WTWNqk1SMmxiMk9qb3lna3FQQ0VGcU9wQ1p6b3EzMzdGZAV96NWJRV0k3R1U3M1NOaDdXRVRB"
},
"next": ""
},
"summary": {
"total_count": 47,
"can_like": true,
"has_liked": false
}
},
"comments": {
"data": [
{
"created_time": "2022-05-25T17:45:47+0000",
"message": "ajkdsfksjdfkljskfdjlker",
"id": ""
},
{
"created_time": "2022-05-25T16:33:31+0000",
"message": "sfjgklsjktjekrjt",
"id": ""
},
{
"created_time": "2022-05-25T16:17:51+0000",
"message": "",
"id": ""
},
{
"created_time": "2022-05-25T16:16:44+0000",
"message": "",
"id": ""
}
],
"summary": {
"order": "ranked",
"total_count": 4,
"can_comment": true
}
},
"id": ""
}
],
"paging": {
"cursors": {
"before": "QVFIUnI4NUFnOGUtSFRnM3lGTTNLWURQQlNnUWlaU1FBdlFwZAlBybXAtTjNNTV9ENUhuRFhLYV9nR2lyZAkVHSVphN09IaTZAFLUctcVpLREtJZAkc0ZAjZAjY0xPWWZbncwUnlrQ2M5ZA09fenlmUVpuSGlCWk41a2ljZATJGMHkwYi1kQndIV0c4bm8yejVub29CNWF3bQZDZD",
"after": "QVFIUkttZAWVDOGNLanBMUzVNSUtfaWRwREQtUzlGUlY0S1hjNFVoRVpFbE1Gc3I1c0pVc3NWZAG1NYlNZASll5empicHhKaUtqZAlM4ampDeU1Hd2VLZVjRVRld4N2pndkNlY0R5ZAGW5rNmpRNlAwWjdXRQZDZD"
},
"next": ""
}
}
}
I want to get the summary (total_count) from both the likes and the comments and the messages as well. So i decided to loop over them and store them as a tuple. The code is as follow:
Code:
import json
import requests
import facebook
graph = facebook.GraphAPI(access_token="")
profile = graph.get_object(id='me', fields='id,name,posts.limit(10)
{likes.summary(true),comments.summary(true)}')
x = json.dumps(profile, indent=4)
# print (x)
raw_data = json.loads(x) # parse the string into a python dictionary
useful_data = raw_data['posts']['data'] # Only picking concerned fields
output = {'day' : []} # This is where we will store the results as tuples of (value, end_time)
for el in useful_data:
# if el['period'] == 'day':
# for i in el['values']: # Get all items in the values array
# output['day'].append( (i['value'], i['end_time']) )
for i in el['likes']:
for j in i['summary']:
output['day'].append( (j['total_count']) )
print(output)
No matter what approach I take I always get the error (string indices must be integers). I even used JSON editor to better understand the JSON data but I still can’t grasp what’s going wrong. Any help would be appreciated.
>Solution :
No matter what approach I take I always get the error (string indices must be integers)
for i in el['likes']:
for j in i['summary']:
output['day'].append( (j['total_count']) )
should be
output['day'].append( el['likes']['summary']['total_count'] )