I have been trying to use a function from populartimes library that returns a json file that includes information regarding a place using this resource link https://github.com/m-wrzr/populartimes.
import populartimes
import pandas as pd
import json
import csv
r = populartimes.get_id("API KEY","ChIJSYuuSx9awokRyrrOFTGg0GY")
display(r)
The output of the function is in json:
{'id': 'ChIJSYuuSx9awokRyrrOFTGg0GY',
'name': 'Gran Morsi',
'address': '22 Warren St, New York, NY 10007, USA',
'types': ['restaurant', 'food', 'point_of_interest', 'establishment'],
'coordinates': {'lat': 40.714315, 'lng': -74.007766},
'rating': 4.4,
'rating_n': 407,
'international_phone_number': '+1 212-577-2725',
'populartimes': [{'name': 'Monday',
'data': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0]},
{'name': 'Tuesday',
'data': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
30,
40,
50,
54,
48,
32,
0]},
{'name': 'Wednesday',
'data': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
31,
43,
52,
53,
45,
32,
0]},
{'name': 'Thursday',
'data': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
49,
69,
80,
78,
62,
39,
0]},
{'name': 'Friday',
'data': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
49,
58,
67,
71,
67,
52,
0]},
{'name': 'Saturday',
'data': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
63,
77,
92,
100,
92,
69,
0]},
{'name': 'Sunday',
'data': [0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0]}],
'time_spent': [90, 210]}
I wish to convert the json to csv file but the usual pandas library is not working properly on the output and returning different errors. I have troubleshooted using different methods.
If I use
df = pd.DataFrame(r)
it returns ValueError "Mixing dicts with non-Series may lead to ambiguous ordering."
If I use
data = json.dumps(r)
df = pd.DataFrame(data[populartimes])
it returns TypeError: string indices must be integers
If I use
data = json.loads(r)
It returns TypeError: the JSON object must be str, bytes or bytearray, not dict
Essentially what I am trying to do is write this json to a csv file in which the headers in json are the columns in the csv file and the values will be under the columns. I wish to make it possible for multiple json entries to the csv files after I am able to write this single json to a csv. Can anyone help in writing this json to a csv file?