Skip to content

hapi.context.getDSTU2FhirContext

Signature: hapi.context.getDSTU2FhirContext(useNarrative*)

Returns: FhirContext fhirContext - the cached FhirContext for DSTU2

Returns the DSTU2 FhirContext for parsing and serializing HAPI resources.

This threadsafe instance is cached, and therefore optimized for use within QIE.

Note

Parsing Usage: var parser = hapi.context.getDSTU2FhirContext().newJsonParser(); var hapiPatient = parser.parseResource(patientJsonString);

Note

Serializing Usage: var parser = hapi.context.getDSTU2FhirContext().newJsonParser(); var patientJsonString = parser.encodeResourceToString(hapiPatient);

Parameters

Type Name Description Default
Boolean useNarrative* (optional) if true, includes a narrative text based on the resource false

Example

// Make sure you've installed the hapi plugin
// Get the HAPI FHIR context for DSTU2
var fhirContext = hapi.context.getDSTU2FhirContext();

// JSON representation of a Patient resource
var patientJson = '' +
   '{' +
   '    "resourceType": "Patient",' +
   '    "id": "example",' +
   '    "name": [' +
   '        {' +
   '            "family": "Doe",' +
   '            "given": ["John"]' +
   '        }' +
   '    ],' +
   '    "gender": "male",' +
   '    "birthDate": "1980-01-01"' +
   '}';

// Use the context to create a parser
var jsonParser = fhirContext.newJsonParser();

// Parse the JSON string into a Patient object
var patient = jsonParser.parseResource(patientJson);

// Extract the patient's name
var humanName = patient.getName().get(0); // Get the first name in the list
var givenNames = humanName.getGiven();    // Get the list of given names
var familyName = humanName.getFamily();   // Get the family name

// Combine given names into a single string
var fullName = givenNames.toArray().join(' ') + ' ' + familyName;