I am fetching data from dashboard as json file using the following code:
ursl='https://xxxxxxxxx'
parameter = {
"access_token": 'ABCD',
"startDate":"2021-05-23",
"endDate":"2021-05-25",
"userId":'ERTI'
}
r = requests.get(url = ursl, params = parameter)
then I save the content as json file then write it to a CSV file:
data=r.content
datajson=r.json()
# opening the csv file in 'w+' mode
file = open('DaTA.csv', 'w+')
# writing the data into the file
with file:
write = csv.writer(file)
write.writerow(datajson)
But I get all my data in one cell only. This is a snapshot of the data:
{'date': {'_when': '2021-05-25', '_date': '2021-05-25T00:00:00.000Z'}, 'timeInterval': '1min', 'userId': '96K7D9', 'id': '60ac1401211c48001774aa3b', 'activities-heart': [{'customHeartRateZones': [], 'dateTime': '2021-05-25', 'heartRateZones': [{'caloriesOut': 993.8944, 'max': 115, 'min': 30, 'minutes': 1067, 'name': 'Out of Range'}, {'caloriesOut': 0, 'max': 142, 'min': 115, 'minutes': 0, 'name': 'Fat Burn'}, {'caloriesOut': 0, 'max': 175, 'min': 142, 'minutes': 0, 'name': 'Cardio'}, {'caloriesOut': 0, 'max': 220, 'min': 175, 'minutes': 0, 'name': 'Peak'}], 'value': '65.31'}], 'activities-heart-intraday':
{'dataset': [{'time': '00:45:00', 'value': 62}, {'time': '00:46:00', 'value': 61}, {'time': '00:47:00', 'value': 61}, {'time': '00:48:00', 'value': 59}, {'time': '00:49:00', 'value': 59}
do you have any suggestion, as I want the data to be tow columns time and value, starting from the dataset word.
I'm not sure if csv writer allows you to do what you need without looping through line by line.
Maybe use pandas
instead?
import pandas as pd
df = pd.DataFrame(datajson['activities-heart-intraday']['dataset'])
df.to_csv("data.csv", index=False)