If you’ve ever wanted to analyze your own health data, here’s how.
- Open the Health app.
- Tap on your profile in the top right.
- Tap Export All Health Data.
- 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.
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.xmlYou should immediately start seeing the data in your terminal.
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.
Thank you. Super useful!