I have the following JSON from facebook:
{
"insights": {
"data": [
{
"name": "page_fans_city",
"period": "day",
"values": [
{
"value": {
"Lakki Marwat, Khyber Pakhtunkhwa, Pakistan": 704,
"Dera Ghazi Khan, Punjab, Pakistan": 725,
"Tank, Khyber Pakhtunkhwa, Pakistan": 750,
"Khwazakhela, Khyber Pakhtunkhwa, Pakistan": 792,
"Dammam, Saudi Arabia": 828,
"Chakdara Fort, Khyber Pakhtunkhwa, Pakistan": 936,
"Gujrat, Punjab, Pakistan": 975,
"Al Ain, United Arab Emirates": 1011,
"Wah, Punjab, Pakistan": 1082,
"Kabul, Afghanistan": 1108,
"Bahawalpur, Punjab, Pakistan": 1144,
"Sargodha, Punjab, Pakistan": 1148,
"Sialkot, Punjab, Pakistan": 1290,
"Parachinar, Federally Administered Tribal Areas, Pakistan": 1322,
"Chitral, Khyber Pakhtunkhwa, Pakistan": 1396,
"Hyderabad, Sindh, Pakistan": 1532,
"Swabi, Khyber Pakhtunkhwa, Pakistan": 1654,
"Hangu, Khyber Pakhtunkhwa, Pakistan": 1680,
"Karak, Khyber Pakhtunkhwa, Pakistan": 1784,
"Doha, Qatar": 1873,
"Charsadda, Khyber Pakhtunkhwa, Pakistan": 1936,
"Sharjah, United Arab Emirates": 2169,
"Gujranwala, Punjab, Pakistan": 2313,
"Nowshera, Khyber Pakhtunkhwa, Pakistan": 2350,
"Dir, Khyber Pakhtunkhwa, Pakistan": 2449,
"Jeddah, Saudi Arabia": 2739,
"Mansehra, Khyber Pakhtunkhwa, Pakistan": 3358,
"Multan, Punjab, Pakistan": 3383,
"Haripur, Khyber Pakhtunkhwa, Pakistan": 3735,
"Faisalabad, Punjab, Pakistan": 3815,
"Quetta, Balochistan, Pakistan": 3862,
"Abu Dhabi, United Arab Emirates": 4374,
"Mingora, Khyber Pakhtunkhwa, Pakistan": 5641,
"Dera Ismail Khan, Khyber Pakhtunkhwa, Pakistan": 6143,
"Bannu, Khyber Pakhtunkhwa, Pakistan": 6623,
"Kohat, Khyber Pakhtunkhwa, Pakistan": 6760,
"Abbottabad, Khyber Pakhtunkhwa, Pakistan": 7204,
"Dubai, United Arab Emirates": 7767,
"Rawalpindi, Punjab, Pakistan": 7869,
"Riyadh, Saudi Arabia": 8477,
"Mardan, Khyber Pakhtunkhwa, Pakistan": 16039,
"Lahore, Punjab, Pakistan": 16371,
"Islamabad, Islamabad Capital Territory, Pakistan": 18291,
"Karachi, Sindh, Pakistan": 22880,
"Peshawar, Khyber Pakhtunkhwa, Pakistan": 93904
},
"end_time": "2022-05-27T07:00:00 0000"
},
{
"value": {
"Lakki Marwat, Khyber Pakhtunkhwa, Pakistan": 703,
"Dera Ghazi Khan, Punjab, Pakistan": 727,
"Tank, Khyber Pakhtunkhwa, Pakistan": 754,
"Khwazakhela, Khyber Pakhtunkhwa, Pakistan": 790,
"Dammam, Saudi Arabia": 829,
"Chakdara Fort, Khyber Pakhtunkhwa, Pakistan": 939,
"Gujrat, Punjab, Pakistan": 978,
"Al Ain, United Arab Emirates": 1013,
"Wah, Punjab, Pakistan": 1086,
"Kabul, Afghanistan": 1113,
"Bahawalpur, Punjab, Pakistan": 1138,
"Sargodha, Punjab, Pakistan": 1145,
"Sialkot, Punjab, Pakistan": 1292,
"Parachinar, Federally Administered Tribal Areas, Pakistan": 1317,
"Chitral, Khyber Pakhtunkhwa, Pakistan": 1398,
"Hyderabad, Sindh, Pakistan": 1537,
"Swabi, Khyber Pakhtunkhwa, Pakistan": 1664,
"Hangu, Khyber Pakhtunkhwa, Pakistan": 1678,
"Karak, Khyber Pakhtunkhwa, Pakistan": 1787,
"Doha, Qatar": 1874,
"Charsadda, Khyber Pakhtunkhwa, Pakistan": 1941,
"Sharjah, United Arab Emirates": 2167,
"Gujranwala, Punjab, Pakistan": 2330,
"Nowshera, Khyber Pakhtunkhwa, Pakistan": 2372,
"Dir, Khyber Pakhtunkhwa, Pakistan": 2454,
"Jeddah, Saudi Arabia": 2732,
"Mansehra, Khyber Pakhtunkhwa, Pakistan": 3359,
"Multan, Punjab, Pakistan": 3380,
"Haripur, Khyber Pakhtunkhwa, Pakistan": 3737,
"Faisalabad, Punjab, Pakistan": 3825,
"Quetta, Balochistan, Pakistan": 3858,
"Abu Dhabi, United Arab Emirates": 4378,
"Mingora, Khyber Pakhtunkhwa, Pakistan": 5646,
"Dera Ismail Khan, Khyber Pakhtunkhwa, Pakistan": 6137,
"Bannu, Khyber Pakhtunkhwa, Pakistan": 6624,
"Kohat, Khyber Pakhtunkhwa, Pakistan": 6747,
"Abbottabad, Khyber Pakhtunkhwa, Pakistan": 7201,
"Rawalpindi, Punjab, Pakistan": 7523,
"Dubai, United Arab Emirates": 7763,
"Riyadh, Saudi Arabia": 8476,
"Mardan, Khyber Pakhtunkhwa, Pakistan": 16058,
"Lahore, Punjab, Pakistan": 16361,
"Islamabad, Islamabad Capital Territory, Pakistan": 18422,
"Karachi, Sindh, Pakistan": 22981,
"Peshawar, Khyber Pakhtunkhwa, Pakistan": 93840
},
"end_time": "2022-05-28T07:00:00 0000"
}
],
"title": "Lifetime Likes by City",
"description": "Lifetime: Aggregated Facebook location data, sorted by city (top 50), about the people who like your Page. (Unique Users)",
"id": ""
}
],
"paging": {
"previous": "",
"next": "?access_token="
}
},
"id": ""
}
As you can see it consists of key value pairs with the city name and the value. What I want is to parse over the key value pairs and insert them through api into the database. The city name will go into the "city" column and the value into the "value" column.
Code:
graph = facebook.GraphAPI(access_token=i)
profile = graph.get_object(id='me', fields='insights.metric(page_fans_city)')
x = json.dumps(profile, indent=4)
# print (x)
# pageID = profile['id']
# pageName = profile['name']
raw_data = json.loads(x) # parse the string into a python dictionary
useful_data = raw_data['insights']['data'] # Only picking concerned fields
output = {'totalCount' : []} # This is where we will store the results as tuples of (value, end_time)
secondOutput = {'newTuple': []}
d = []
for el in useful_data:
if el['period'] == "day":
for i in el['values']:
output['totalCount'].append( (i['value'], i['end_time']) )
print (output)
The above code stores the entire value, what I want are the individual key value pairs so that I can work with them. Any help would be appreciated.
CodePudding user response:
In your inner loop, you can iterate over i['value']
as follows:
for key, value in i['value'].items():
# do something with the data
For more information, see the data structures section of The Python Tutorial in the Python 3 docs.