Exercise Solution: A Patient Resource

Let’s go through the process of creating a FHIR-compliant Patient resource. This includes writing or generating the Patient resource, validating it, and also validating the resource using any available method. A Patient resource represents patient information using the standard FHIR structure.

First, we will write the Patient resource or use a tool to generate it.

Next, we will check if it’s correct using the official FHIR validator or with an IDE.

1. Writing or Generating the Patient Resource

We’re going to be looking at three ways in which you can write or generate a FHIR Patient resource

  • Use a tool like ChatGPT to generate a sample
  • Refer to an example from the FHIR documentation
  • Manually write the JSON structure

Using ChatGPT to generate a patient resource

One of the quickest ways to generate a FHIR Patient resource is by using any generative AI model like ChatGPT. Simply open a chat and ask, “Create a valid FHIR Patient Resource.” Within seconds, you’ll have a properly structured example ready to use. This is a good option if you want to get started quickly without writing it from scratch.

Below is an example of a ChatGPT generated resource :

{
  "resourceType": "Patient",
  "name": [
    {
      "given": ["John"]
    }
  ],
  "telecom": [
    {
      "system": "email",
      "value": "john@example.com"
    }
  ]
}

Referring to an example from the FHIR Documentation

You can explore the FHIR Patient Examples documentation to see a wide range of sample patient resources. These examples cover different use cases, such as handling names, contact details, identifiers, and other patient-specific data. Reviewing them can give you a clear understanding of how the FHIR Patient resource is structured and how it can be adapted to different healthcare scenarios.

Below is an example of a FHIR Patient Resource from their examples documentation:

Source

{
  "resourceType" : "Patient",
  "id" : "xcda",
  "text" : {
    "status" : "generated",
    "div" : "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p style=\"border: 1px #661aff solid; background-color: #e6e6ff; padding: 10px;\"><b>John Doe </b> male, DoB: 1932-09-24 ( Medical record number: 12345\u00a0(use:\u00a0USUAL))</p><hr/><table class=\"grid\"><tr><td style=\"background-color: #f3f5da\" title=\"Record is active\">Active:</td><td colspan=\"3\">true</td></tr><tr><td style=\"background-color: #f3f5da\" title=\"Patient Links\">Links:</td><td colspan=\"3\"><ul><li>Managing Organization: <a href=\"organization-example-good-health-care.html\">Organization/2.16.840.1.113883.19.5: Good Health Clinic</a> &quot;Good Health Clinic&quot;</li></ul></td></tr></table></div>"
  },
  "identifier" : [{
    "use" : "usual",
    "type" : {
      "coding" : [{
        "system" : "http://terminology.hl7.org/CodeSystem/v2-0203",
        "code" : "MR"
      }]
    },
    "system" : "urn:oid:2.16.840.1.113883.19.5",
    "value" : "12345"
  }],
  "active" : true,
  "name" : [{
    "family" : "Doe",
    "given" : ["John"]
  }],
  "gender" : "male",
  "birthDate" : "1932-09-24",
  "managingOrganization" : {
    "reference" : "Organization/2.16.840.1.113883.19.5",
    "display" : "Good Health Clinic"
  }
}

Manually Write the Patient Resource in JSON using an IDE

You can manually create a FHIR Patient resource by writing it in JSON format using any code editor or IDE, such as VS Code or Cursor. This approach helps you understand the structure of the resource and the required fields.

Start by defining the resourceType as "Patient", and then add relevant fields like name, telecom, gender, and birthDate. Most IDEs support JSON syntax highlighting, and you can also configure them to use the FHIR JSON Schema, which will provide auto-completion, field suggestions, and validation support as you type.

This hands-on method gives you full control over the structure and content of your resource. In the next section, we’ll walk you through how to set up validation in your IDE to guide you as you build.

2. Validating the Resource

Once you’ve created your Patient resource, it’s important to check that it follows the FHIR specification.You can validate your FHIR Patient resource in two ways :

  • Using official FHIR validation Tools
  • Using an IDE like VSCode or Cursor.

Validating a FHIR Patient resource using an official tool

You can validate your FHIR Patient tool using the FHIR JSON Validator. Just paste your JSON into the tool, and it will report any errors or warnings.

  • Errors indicate that the resource does not conform to the standard and must be fixed.
  • Warnings (e.g., missing text narrative) are not critical but may affect how the resource is displayed or interpreted.

You can also explore Inferno, an open-source testing tool for FHIR APIs that includes validation against multiple real-world use cases.

Validating in an IDE Using JSON Schema

You can set up validation in your code editor if you prefer to work locally. Follow the given steps and you can validate your FHIR Patient resource with an IDE.

1. Download the FHIR JSON Schema from the official FHIR documentation.

Open this link, and follow the GIF tutorial below to download the FHIR JSON Schema validation zip file

How to download the FHIR Schema

2. Configure your IDE

Open VsCode and create a .vscode folder in your project directory. Inside .vscode, create a file named settings.json and add the following:

{
  "json.schemas": [
    {
      "fileMatch": ["*.json"],
      "url": "./fhir.schema.json"
    }
  ]
}

Copy the fhir.schema.json file into the root of your folder. The project structure must look like this:

.vscode/
  └── settings.json
fhir.schema.json
patient.json

You have successfully configured your IDE to validate any .json file in your root folder to follow the rules contained in the fhir.schema.json.

3. Write a FHIR Patient Resource

Now that your IDE is configured, open the patient.json file and start writing a patient resource.

The schema will provide field validation and auto-completion based on the FHIR specification.

Example with schema-enabled validation:

{
  "resourceType": "Patient",
  "name": [{ "given": ["John"] }],
  "telecom": [
    { "system": "email", "value": "john@example.com" }
  ]
}

If you add an invalid field like:

{
  "hello": "world"
}

The IDE will indicate that the property is not allowed based on the schema rules.

Summary

In this lesson, you learned how to:

  • Create a basic Patient resource
  • Validate it using the official FHIR validator
  • Set up JSON Schema validation in your IDE

You can now experiment with other resource types or extend the patient data with additional fields such as address, gender, birth date, or identifiers. This will help you better understand how flexible and detailed FHIR resources can be.

Once your resource is ready, make sure to validate it using one of the methods discussed. After that, feel free to save your work and share your Patient resource in the comments section. This will not only help others learn but also give you a chance to receive feedback and suggestions.

Comments (0)

No comments yet. Be the first to comment!