I try to get campaigns data from facebook ads with graph API, and put the data on Pandas Dataframe.
So I got the data, but I don’t know how I can to extract these data from json.
I tried this code, but now I have no idea that how I can proceed.
import requests
import pandas as pd
import json
graphAPI = "https://graph.facebook.com/v3.3/#/ads?fields=name,insights{reach,impressions,clicks,frequency,cpm,cpc},ads{insights.time_range({'since':'2019-06-01','until':'2019-06-30'}).time_increment(1)}&access_token=#"
req = requests.get(graphAPI)
ReqDict = req.json()
print(ReqDict)
So, I got this result:
{'data': [
{'name': 'inverno_0160702', 'id': '213544564564'},
{'name': 'instagram_ads_conversao_postinsta_post2_adidasoriginals_smith',
'insights':
{'data':
[
{'reach': '2619',
'impressions': '2625',
'clicks': '43',
'frequency': '1.002291',
'cpm': '9.939048',
'cpc': '0.606744',
'date_start': '2019-06-02',
'date_stop': '2019-07-01'}
],
'paging': {'cursors': {'before': 'MAZDZD', 'after': 'MAZDZD'}}}, 'id': '23843373097230145'},
{'name': 'instagram_ads_conversao_postinsta_', 'id': '2256589465461212'},
{'name': 'instagram_ads_conversao_postinsta', 'id': '23123546545644546'},
{'name': 'instagram_ads_conversao_postinsta_20190628',
'insights':
{'data':
[
{'reach': '23610',
'impressions': '37099',
'clicks': '1815',
'frequency': '1.571326',
'cpm': '4.492574',
'cpc': '0.091829',
'date_start': '2019-06-02',
'date_stop': '2019-07-01'}
],
'paging': {'cursors': {'before': 'MAZDZD', 'after': 'MAZDZD'}}}, 'id': '2132653545313545313222'}],
'paging': {'cursors': {'before':'QVFIUlBJdHFYY1RqYnk3TTFSUDVQemh0bTBXY1BrazdrWXY2WTI5LXc5R2hUVTdnWnRiYzNnTl96azdjVWZAxamcycVVCOXM4ZAUJidV9HUzlUYUNuV25PQ0x3', 'after': 'QVFIUldvei1tRTZAUVGk1N3hhQTJUX1dQbWVSSnV0d0dTY0ctTmQ0ZAnFRdlg4NTVFbHNrazVUZA2NqTk5aMVI2UVdjM0dWUWltenVlY3Rna0N4aFdNeHA1SFRn'}, 'next': 'https://graph.facebook.com/v3.3/#'}
}
I would like to represents my dataframe like this:
Name | id | reach | impressions | Clicks | frequency | cpm | cpc | date_start | date_stop
inverno... |null | 2619 | 26554 | 43 | 1.002 | 9.93 | 0.60 | '2019-06-02'| '2019-06-02'
instagram_ads... |222562..| null | null | null | null | null | null | null | null
instagram_ads... |null | 23610 | 37099 | 1815 | 1.571326 | 4.49 | 0.09 | '2019-06-02'| '2019-07-01'
instagram_ads... |231235..| null | null | null | null | null | null | null | null
In some keys of this json has no the atribute insights
but it’s not a problem, the values can be null.
Someone can help me with this question. I’m beginner.
Edit
Now I try this, but doesn’t work, the columns was created but the data came as null.
jsonDf = json_normalize(ReqDict,record_path='data',meta=['reach','impressions','clicks','frequency','cpm','cpc','date_start','date_stop'], errors='ignore')
Result
id insights name reach impressions clicks frequency cpm cpc date_start date_stop
23843368620640145 {'data': [{'reach': '6726', 'impressions': '79... facebook_ads_trafego_singlead_LKL_promocionado... NaN NaN NaN NaN NaN NaN NaN NaN
23843337666290145 {'data': [{'reach': '12797', 'impressions': '1... facebook_ads_trafego_singlead_LKL_inverno19_fe... NaN NaN NaN NaN NaN NaN NaN NaN
23843339836870145 {'data': [{'reach': '24720', 'impressions': '2... facebook_ads_trafego_singlead_LKL_promocionado... NaN NaN NaN NaN NaN NaN NaN NaN
23843337719810145 {'data': [{'reach': '7766', 'impressions': '88... facebook_ads_trafego_singlead_LKL_fitness_femi... NaN NaN NaN NaN NaN NaN NaN NaN
23843337726230145 {'data': [{'reach': '579459', 'impressions': '... facebook_ads_trafego_singlead_LKL_fitness_femi... NaN NaN NaN NaN NaN NaN NaN NaN
Thanks!!!
2
Answers
So I got this solution:
pandas.read_json(ReqDict)
Normalize in nice flat form – use json_normalize(ReqDict)