Skip to content

Instantly share code, notes, and snippets.

@alifeee
Last active February 1, 2024 01:43
Show Gist options
  • Save alifeee/5ccfdff391f1bfc894f6e21beb78996d to your computer and use it in GitHub Desktop.
Save alifeee/5ccfdff391f1bfc894f6e21beb78996d to your computer and use it in GitHub Desktop.
Toggl Track API JSON to CSV conversion
#!/bin/bash
# turns Toggl JSON API data into CSV format
# currently ignores any array fields
# you can override the TOGGL_KEYS env variable to set which keys you want in the CSV
# usage
# get toggl data
# https://developers.track.toggl.com/docs/api/time_entries/index.html
# curl https://api.track.toggl.com/api/v9/me/time_entries -H "Content-Type: application/json" -u <email>:<password> > toggl.json
# curl https://api.track.toggl.com/api/v9/me/time_entries -H "Content-Type: application/json" -u <api token>:api_token > toggl.json
# get January entries, e.g.,
# curl "https://api.track.toggl.com/api/v9/me/time_entries?start_date=2024-01-01&end_date=2024-02-01" -H "Content-Type: application/json" -u <api token>:api_token > 2024-01_toggl.json
# script:
# as argument:
# ./parse_toggl.sh toggl.json
# as pipe
# cat toggl.json | /parse_toggl.sh
# or just directly pipe curl output to the script (use "-s" with curl to suppress loading output to stderr)
# from https://gist.github.com/alifeee/5ccfdff391f1bfc894f6e21beb78996d
KEYS="${TOGGL_KEYS:-.id, .workspace_id, .project_id, .task_id, .billable, .start, .stop, .duration, .description, .duronly, .at, .server_deleted_at, .user_id, .uid, .wid, .pid}"
# print CSV headers
echo $KEYS | awk -F', ' '{for (i=1; i<NF; i++) {printf "%s,", substr($i, 2)}} END {printf "\n"}'
# print CSV content (parse JSON with jq)
cat $1 | jq -r '.[] | ['"${KEYS}"'] | @csv'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment