{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/martybr/ExferoHealth/blob/master/docs/export.schema.json",
  "title": "Exfero Health JSON export",
  "description": "A Exfero Health JSON export, schema version 8. See export-format.md for what each field means.",
  "type": "object",
  "required": [
    "schemaVersion",
    "generatedAt",
    "timeZone",
    "range",
    "recordCount",
    "records",
    "deleted",
    "gaps",
    "gapCheck"
  ],
  "additionalProperties": false,
  "properties": {
    "schemaVersion": {
      "const": 8,
      "description": "Bumped whenever the structure changes."
    },
    "generatedAt": {
      "$ref": "#/$defs/timestamp",
      "description": "When the export was made."
    },
    "timeZone": {
      "type": "string",
      "description": "The exporting iPhone's time zone, an IANA identifier such as Europe/Amsterdam."
    },
    "range": {
      "type": "object",
      "required": [
        "start",
        "end"
      ],
      "additionalProperties": false,
      "description": "The period the export covers; end is exclusive.",
      "properties": {
        "start": {
          "$ref": "#/$defs/timestamp"
        },
        "end": {
          "$ref": "#/$defs/timestamp"
        }
      }
    },
    "recordCount": {
      "type": "integer",
      "minimum": 0,
      "description": "The number of items in records."
    },
    "records": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/record"
      }
    },
    "deleted": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/deletedRecord"
      },
      "description": "Samples deleted from Health since the previous export; only filled for the Since last export rule."
    },
    "gaps": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/gap"
      }
    },
    "gapCheck": {
      "oneOf": [
        {
          "$ref": "#/$defs/gapCheck"
        },
        {
          "type": "null"
        }
      ],
      "description": "What the gap check judged; null when it didn't run."
    }
  },
  "$defs": {
    "timestamp": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$",
      "description": "ISO 8601 in UTC with milliseconds."
    },
    "uuid": {
      "type": "string",
      "pattern": "^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$"
    },
    "metric": {
      "enum": [
        "stepCount",
        "distanceWalkingRunning",
        "flightsClimbed",
        "activeEnergyBurned",
        "basalEnergyBurned",
        "appleExerciseTime",
        "appleStandTime",
        "appleStandHour",
        "distanceCycling",
        "distanceSwimming",
        "swimmingStrokeCount",
        "physicalEffort",
        "runningSpeed",
        "runningPower",
        "runningStrideLength",
        "runningGroundContactTime",
        "runningVerticalOscillation",
        "workout",
        "workoutEffortScore",
        "estimatedWorkoutEffortScore",
        "cyclingSpeed",
        "cyclingPower",
        "cyclingCadence",
        "cyclingFunctionalThresholdPower",
        "distanceRowing",
        "rowingSpeed",
        "distancePaddleSports",
        "paddleSportsSpeed",
        "distanceSkatingSports",
        "distanceCrossCountrySkiing",
        "crossCountrySkiingSpeed",
        "underwaterDepth",
        "waterTemperature",
        "walkingSpeed",
        "walkingStepLength",
        "walkingAsymmetryPercentage",
        "walkingDoubleSupportPercentage",
        "appleWalkingSteadiness",
        "stairAscentSpeed",
        "stairDescentSpeed",
        "sixMinuteWalkTestDistance",
        "heartRate",
        "restingHeartRate",
        "walkingHeartRateAverage",
        "heartRateVariabilitySDNN",
        "bloodPressureSystolic",
        "bloodPressureDiastolic",
        "vo2Max",
        "heartRateRecoveryOneMinute",
        "bodyMass",
        "height",
        "bodyMassIndex",
        "bodyFatPercentage",
        "respiratoryRate",
        "oxygenSaturation",
        "sleepAnalysis",
        "appleSleepingWristTemperature",
        "appleSleepingBreathingDisturbances",
        "sleepApneaEvent",
        "dietaryEnergyConsumed",
        "dietaryProtein",
        "dietaryCarbohydrates",
        "dietaryFatTotal",
        "dietarySugar",
        "dietaryFiber",
        "dietarySodium",
        "dietaryCaffeine",
        "numberOfAlcoholicBeverages",
        "dietaryWater",
        "environmentalAudioExposure",
        "headphoneAudioExposure",
        "menstrualFlow",
        "intermenstrualBleeding",
        "ovulationTestResult",
        "cervicalMucusQuality"
      ],
      "description": "Exfero Health's name for a Health data type; see the data type table in export-format.md."
    },
    "record": {
      "type": "object",
      "required": [
        "uuid",
        "type",
        "value",
        "unit",
        "categoryValue",
        "startDate",
        "endDate",
        "timeZone",
        "source",
        "sourceBundleId",
        "deviceModel",
        "attributes"
      ],
      "additionalProperties": false,
      "properties": {
        "uuid": {
          "$ref": "#/$defs/uuid",
          "description": "HealthKit's ID for the sample; stable across exports."
        },
        "type": {
          "$ref": "#/$defs/metric"
        },
        "value": {
          "type": "number",
          "description": "In unit; for category data and workouts, the duration in minutes."
        },
        "unit": {
          "type": "string"
        },
        "categoryValue": {
          "type": [
            "string",
            "null"
          ],
          "description": "HealthKit's name for a category value, or a workout's activity type."
        },
        "startDate": {
          "$ref": "#/$defs/timestamp"
        },
        "endDate": {
          "$ref": "#/$defs/timestamp"
        },
        "timeZone": {
          "type": [
            "string",
            "null"
          ],
          "description": "The time zone the sample was recorded in, when HealthKit knows it."
        },
        "source": {
          "type": "string",
          "description": "The source's display name, which the user can change."
        },
        "sourceBundleId": {
          "type": [
            "string",
            "null"
          ],
          "description": "The source's bundle identifier, which stays the same."
        },
        "deviceModel": {
          "type": [
            "string",
            "null"
          ],
          "description": "The hardware model, such as Watch7,3."
        },
        "attributes": {
          "type": "object",
          "additionalProperties": {
            "type": "number"
          },
          "description": "Extra values: workout energy, distance and heart rate; menstrual flow cycleStart. Empty otherwise."
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "stepCount"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "distanceWalkingRunning"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "flightsClimbed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "activeEnergyBurned"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "kcal"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "basalEnergyBurned"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "kcal"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "appleExerciseTime"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "appleStandTime"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "appleStandHour"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "distanceCycling"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "distanceSwimming"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "m"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "swimmingStrokeCount"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "physicalEffort"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "kcal/hr*kg"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "runningSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km/hr"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "runningPower"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "W"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "runningStrideLength"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "m"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "runningGroundContactTime"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "ms"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "runningVerticalOscillation"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "cm"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "workout"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "workoutEffortScore"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "appleEffortScore"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "estimatedWorkoutEffortScore"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "appleEffortScore"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "cyclingSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km/hr"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "cyclingPower"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "W"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "cyclingCadence"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count/min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "cyclingFunctionalThresholdPower"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "W"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "distanceRowing"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "rowingSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km/hr"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "distancePaddleSports"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "paddleSportsSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km/hr"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "distanceSkatingSports"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "distanceCrossCountrySkiing"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "crossCountrySkiingSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km/hr"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "underwaterDepth"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "m"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "waterTemperature"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "degC"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "walkingSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "km/hr"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "walkingStepLength"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "cm"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "walkingAsymmetryPercentage"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "walkingDoubleSupportPercentage"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "appleWalkingSteadiness"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "stairAscentSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "m/s"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "stairDescentSpeed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "m/s"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "sixMinuteWalkTestDistance"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "m"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "heartRate"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count/min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "restingHeartRate"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count/min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "walkingHeartRateAverage"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count/min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "heartRateVariabilitySDNN"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "ms"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "bloodPressureSystolic"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "mmHg"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "bloodPressureDiastolic"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "mmHg"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "vo2Max"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "ml/kg*min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "heartRateRecoveryOneMinute"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count/min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "bodyMass"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "kg"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "height"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "cm"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "bodyMassIndex"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "bodyFatPercentage"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "respiratoryRate"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count/min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "oxygenSaturation"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "%"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "sleepAnalysis"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "appleSleepingWristTemperature"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "degC"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "appleSleepingBreathingDisturbances"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count/hr"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "sleepApneaEvent"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietaryEnergyConsumed"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "kcal"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietaryProtein"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "g"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietaryCarbohydrates"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "g"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietaryFatTotal"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "g"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietarySugar"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "g"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietaryFiber"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "g"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietarySodium"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "mg"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietaryCaffeine"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "mg"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "numberOfAlcoholicBeverages"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "count"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "dietaryWater"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "mL"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "environmentalAudioExposure"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "dBASPL"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "headphoneAudioExposure"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "dBASPL"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "menstrualFlow"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "intermenstrualBleeding"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "ovulationTestResult"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "cervicalMucusQuality"
              }
            }
          },
          "then": {
            "properties": {
              "unit": {
                "const": "min"
              }
            }
          }
        }
      ]
    },
    "deletedRecord": {
      "type": "object",
      "required": [
        "uuid",
        "type"
      ],
      "additionalProperties": false,
      "properties": {
        "uuid": {
          "$ref": "#/$defs/uuid"
        },
        "type": {
          "$ref": "#/$defs/metric"
        }
      }
    },
    "gap": {
      "type": "object",
      "required": [
        "type",
        "kind",
        "start",
        "end",
        "minutes"
      ],
      "additionalProperties": false,
      "properties": {
        "type": {
          "$ref": "#/$defs/metric"
        },
        "kind": {
          "enum": [
            "noData",
            "duringSleep",
            "unstagedSleep",
            "shortSleep"
          ]
        },
        "start": {
          "$ref": "#/$defs/timestamp"
        },
        "end": {
          "$ref": "#/$defs/timestamp"
        },
        "minutes": {
          "type": [
            "number",
            "null"
          ],
          "description": "Time asleep, for shortSleep."
        }
      }
    },
    "gapCheck": {
      "type": "object",
      "required": [
        "periods",
        "missingDataMetrics",
        "duringSleepMetrics",
        "minimumSleepGapMinutes",
        "frequentSpacingMinutes",
        "shortSleepMinutes"
      ],
      "additionalProperties": false,
      "properties": {
        "periods": {
          "enum": [
            "days",
            "nights"
          ],
          "description": "Midnight to midnight, or noon to noon."
        },
        "missingDataMetrics": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/metric"
          }
        },
        "duringSleepMetrics": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/metric"
          }
        },
        "minimumSleepGapMinutes": {
          "type": "integer"
        },
        "frequentSpacingMinutes": {
          "type": "integer"
        },
        "shortSleepMinutes": {
          "type": "integer"
        }
      }
    }
  }
}
