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 a very useful approach, especially from the search perspective. This blog post shows how to create a new handlebar template for the FHIR converter which creates FHIR observation resources based on Apple Health Data.

Handlebar template for FHIR converter

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

Patient template

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

CDA Patient input

Handlebar Template

Name this template "Sections/Patient.hbs"

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

FHIR Patient output

Observation template

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

CDA Observation input

Handlebar Template

Name this template "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)}}
                {{/with}}
        {{/if}}
    {{/each}}
{{/each}}

FHIR Observation output

Main template

The main template constructs the FHIR bundle resource which has multiple entries. In this case, entries (Patient and Observation) are populated in sub-templates.

{
    "resourceType": "Bundle",
    "type": "batch",
    "entry": [  
        {{>Sections/Patient.hbs}}
        {{>Sections/Observation.hbs}}
      ]
}

In FHIR Converter everything looks like this:

The templates mentioned in this blog post are available also from my Github repository.

Testing

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

Sending individual observations, to the Observation endpoint

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

Comments