Apple Health CDA data conversion to FHIR with Handlebar engine

Previous blog post I showed how to export Apple Health data to the Azure API for FHIR as an attachment document. This is not very useful approach especially from the search perspective. This blog post shows how to create a new handlebar template for FHIR converter which creates FHIR observation resources based on Apple Health Data.

Handlebar template for FHIR converter

Let's first create a one template for handling Patient resource and one for Observations.

Patient template

Patient template retrieves Patient data from CDA document's patient role section and converts it to FHIR patient resource.

CDA Patient input


Handlebar Template

Name this template as "Sections/Patient.hbs"

{{#with msg.ClinicalDocument.recordTarget.patientRole}}
    {{#with (evaluate 'Utils/GeneratePatientId.hbs' obj=this) as |patientId|}}
        {{>Resources/Patient.hbs patientRole=.. ID=patientId.Id}}

FHIR Patient output


Observation template

Observation template loops through Observations which are located under Entry/Organizer/Component.

CDA Observation input


Handlebar Template

Name this template as "Sections/Observation.hbs"

{{#each (toArray msg.ClinicalDocument.entry) as |drEntry|}}
    {{#each (toArray this.organizer.component) as |obsEntry|}}
        {{#if obsEntry.observation}}
            {{>Resources/Observation.hbs observationCategory="vital-signs" observationEntry=obsEntry.observation ID=(generateUUID (toJsonString obsEntry.observation))}},                   
                {{#with (evaluate 'Utils/GeneratePatientId.hbs' obj=../../msg.ClinicalDocument.recordTarget.patientRole) as |patientId|}}
                    {{>References/Observation/subject.hbs ID=(generateUUID (toJsonString obsEntry.observation)) REF=(concat 'Patient/' patientId.Id)}}

FHIR Observation output


Main template

Main template constructs FHIR bundle resource which has multiple entries. In this case entries (Patient and Observation) are populated in sub templates.

    "resourceType": "Bundle",
    "type": "batch",
    "entry": [  

In FHIR Converter everything looks like this:


Templates mentioned in this blog post founds also from my Github repository.


You can send the bundle resource directly to the Bundle endpoint of the Azure API for FHIR but better option is to send Patient and Observation resource individually. I mean that Patient resource is sent to Patient endpoint and Observations to the Observation endpoint.

Sending individual observation to the Observation endpoint


After that you can easily find ex. specific patient's all observations.