How to loop over json array from facebook graph API

Question:

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.

Asked By: Fakhr Ali

||

Answers:

How to iterate the json, this is one way you could do it:

Data:

test = {
    "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"
            }
        ]
    }
}

Code:

for data in test["insights"]["data"]:
    print(data["title"])
    for value in data["values"]:
        print(value)

Result:

Daily Total Impressions
{'value': 14, 'end_time': '2022-05-16T07:00:00+0000'}
{'value': 17, 'end_time': '2022-05-17T07:00:00+0000'}
Weekly Total Impressions
{'value': 14, 'end_time': '2022-05-16T07:00:00+0000'}
{'value': 31, 'end_time': '2022-05-17T07:00:00+0000'}
28 Days Total Impressions
{'value': 14, 'end_time': '2022-05-16T07:00:00+0000'}
{'value': 31, 'end_time': '2022-05-17T07:00:00+0000'}

If you need to unpack it even further:

for data in test["insights"]["data"]:
    print(data["title"])
    for val in data["values"]:
        for key, value in val.items():
            print(f"The value: [ {value} ]")

Result:

Daily Total Impressions
The value: [ 14 ]
The value: [ 2022-05-16T07:00:00+0000 ]
The value: [ 17 ]
The value: [ 2022-05-17T07:00:00+0000 ]
Weekly Total Impressions
The value: [ 14 ]
The value: [ 2022-05-16T07:00:00+0000 ]
The value: [ 31 ]
The value: [ 2022-05-17T07:00:00+0000 ]
28 Days Total Impressions
The value: [ 14 ]
The value: [ 2022-05-16T07:00:00+0000 ]
The value: [ 31 ]
The value: [ 2022-05-17T07:00:00+0000 ]
Answered By: user56700
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... 
Answered By: perpetualstudent