'How to loop over json array from facebook graph API
How can I loop over a json array that looks like the one below, using python? {
"insights": {
"data": [
{
"name": "page_impressions",
"period": "day",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00+0000"
},
{
"value": 17,
"end_time": "2022-05-17T07:00:00+0000"
}
],
"title": "Daily Total Impressions",
"description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/day"
},
{
"name": "page_impressions",
"period": "week",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00+0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00+0000"
}
],
"title": "Weekly Total Impressions",
"description": "Weekly: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/week"
},
{
"name": "page_impressions",
"period": "days_28",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00+0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00+0000"
}
],
"title": "28 Days Total Impressions",
"description": "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/days_28"
}
]
I know how to loop over individual items:
values = profile['insights']['data'][0]['values'][0]
But this isn't a feasible solution considering that I need to loop over every item and display the output and store it. Any help would be appreciated.
Solution 1:[1]
profile = {
"insights": {
"data": [
{
"name": "page_impressions",
"period": "day",
"values": [
{
"value": 14,
"end_time": "2022-05-16T07:00:00+0000"
},
{
"value": 17,
"end_time": "2022-05-17T07:00:00+0000"
}
],
"title": "Daily Total Impressions",
"description": "Daily: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/day"
},
{
"name": "page_impressions",
"period": "week",
"values": [
{
"value": 15,
"end_time": "2022-05-16T07:00:00+0000"
},
{
"value": 31,
"end_time": "2022-05-17T07:00:00+0000"
}
],
"title": "Weekly Total Impressions",
"description": "Weekly: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/week"
},
{
"name": "page_impressions",
"period": "days_28",
"values": [
{
"value": 16,
"end_time": "2022-05-16T07:00:00+0000"
},
{
"value": 33,
"end_time": "2022-05-17T07:00:00+0000"
}
],
"title": "28 Days Total Impressions",
"description": "28 Days: The number of times any content from your Page or about your Page entered a person's screen. This includes posts, stories, ads, as well other content or information on your Page. (Total Count)",
"id": "/insights/page_impressions/days_28"
}
]}}
There are 2 ways to do it:
Option (1) fixed schema
result = []
for i in range(3):
temp = []
for j in range(2):
temp.append(profile['insights']['data'][i]['values'][j])
result.append(temp)
print(result)
Option (2) for any schema
def get_vals(nested, key):
result = []
if isinstance(nested, list) and nested != []: #non-empty list
for lis in nested:
result.extend(get_vals(lis, key))
elif isinstance(nested, dict) and nested != {}: #non-empty dict
for val in nested.values():
if isinstance(val, (list, dict)): #(list or dict) in dict
result.extend(get_vals(val, key))
if key in nested.keys(): #key found in dict
result.append(nested[key])
return result
result = get_vals(profile, 'values')
print(result)
Both options will output:
[[{'value': 14, 'end_time': '2022-05-16T07:00:00+0000'},
{'value': 17, 'end_time': '2022-05-17T07:00:00+0000'}],
[{'value': 15, 'end_time': '2022-05-16T07:00:00+0000'},
{'value': 31, 'end_time': '2022-05-17T07:00:00+0000'}],
[{'value': 16, 'end_time': '2022-05-16T07:00:00+0000'},
{'value': 33, 'end_time': '2022-05-17T07:00:00+0000'}]]
After that you could place them in a DataFrame
import pandas as pd
df = pd.DataFrame(result).T
df.columns = ['Daily Total Impressions', 'Weekly Total Impressions', '28 Days Total Impressions']
Output
Daily Total Impressions \
0 {'value': 14, 'end_time': '2022-05-16T07:00:00...
1 {'value': 17, 'end_time': '2022-05-17T07:00:00...
Weekly Total Impressions \
0 {'value': 15, 'end_time': '2022-05-16T07:00:00...
1 {'value': 31, 'end_time': '2022-05-17T07:00:00...
28 Days Total Impressions
0 {'value': 16, 'end_time': '2022-05-16T07:00:00...
1 {'value': 33, 'end_time': '2022-05-17T07:00:00...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | perpetual student |
