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 into JSON.

Save the following in 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 easily turn the JSON into 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.

Happy exploring!

Home