Skip to content

Instantly share code, notes, and snippets.

@lo-scozzese
Last active January 14, 2024 12:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lo-scozzese/80fa2f0170b02c6de95d5fdf820d39c9 to your computer and use it in GitHub Desktop.
Save lo-scozzese/80fa2f0170b02c6de95d5fdf820d39c9 to your computer and use it in GitHub Desktop.
Python script to convert Garmin weight data from JSON format to CSV, and also display a matplotlib chart
#%% import dependencies
# Python script to convert Garmin weight data from JSON format to CSV, and also display a matplotlib chart.
# Save Garmin data as 'garmin.json' in the same directory as this python file.
import json, time, os, sys
import datetime as dt
from matplotlib import pyplot as plt
#%% create a URL, so the user can download the data from Garmin:
ts = str(int(time.time())) + '000'
garmin_url = "https://connect.garmin.com/modern/proxy/userprofile-service/userprofile/personal-information/weightWithOutbound/filterByDay?from=1293840000000&until=" + ts
print('\nFor latest data, paste this URL into your browser, and save contents as garmin.json:')
print('\n' + garmin_url)
#%% load the data in the *.json file downloaded from Garmin:
destination_path = r"garmin.json"
if not os.path.lexists(destination_path):
print ('\nFILE DOES NOT EXIST: {}\n'.format(destination_path))
sys.exit()
with open(destination_path, "r") as read_file:
data = json.load(read_file)
garmin_dates = []
garmin_weights = []
for entry in data:
garmin_dates.append(time.strftime('%Y-%m-%d', time.localtime(int(0.001*entry['date']))))
garmin_weights.append(round(0.001*entry['weight'],1))
#%% save the data in a CSV file:
with open('garmin weight.csv', 'w') as file_handler: # a = append, w = overwrite
for dates, weights in zip(garmin_dates, garmin_weights):
file_handler.write(dates + ',' + str(weights) + '\n')
#%% create plot:
x = [dt.datetime.strptime(d,'%Y-%m-%d').date() for d in garmin_dates]
fig, ax = plt.subplots()
ax.plot(x, garmin_weights)
ax.set_ylabel('weight (kg)')
ax.set_title("My weight")
plt.grid(True)
plt.tight_layout()
@Mark10
Copy link

Mark10 commented Apr 11, 2019

Hi,

thanks for your work. Could you add the missing fields from Garmin or an example how to handle "empty" data.
Since I'm happy Index Smart Scale user there are some more interesting data fields.
I've tried to add the fields straight forward to your code, but I'll receive an error when there are "empty" entries from the time
before I used the Index Smart Scale.
eg: garmin_knochenmasse.append(round(0.001*entry['boneMass'],1))
-> TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

Thanks

Mark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment