Skip to content

Instantly share code, notes, and snippets.

@hoffa
Last active April 20, 2024 18:45
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoffa/936db2bb85e134709cd263dd358ca309 to your computer and use it in GitHub Desktop.
Save hoffa/936db2bb85e134709cd263dd358ca309 to your computer and use it in GitHub Desktop.

Parsing Apple Health data

If you’ve ever wanted to analyze your own health data, here’s how.

Exporting as XML

  1. Open the Health app.
  2. Tap on your profile in the top right.
  3. Tap Export All Health Data.
  4. Share the archive with yourself (e.g. via AirDrop, Files, Mail, etc.).

Within the archive, you’ll find export.xml. This is where the main metrics are stored.

Converting to JSON

If you open export.xml, you'll see most of the interesting data is contained in the attributes of Record elements. So let's make a small Python script to convert those attributes to JSON.

Save the following to a file called parse.py:

import json
import sys
from xml.etree.ElementTree import iterparse

for _, elem in iterparse(sys.argv[1]):
    if elem.tag == "Record":
        print(json.dumps(elem.attrib))

Then run:

python parse.py export.xml

You should immediately start seeing the data in your terminal.

Converting to CSV

Using jq, we can convert the JSON to CSV:

python parse.py export.xml | jq -r '[.endDate, .type, .unit, .value] | @csv'

If you prefer TSV (e.g. for processing with cut), replace @csv by @tsv.

Save the data to a file and analyze with your favorite software.

@delenamalan
Copy link

Thank you. Super useful!

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