Flattening QuestionnaireResponses
# support
k
Howdy! As we know, Questionnaires can get pretty large and complex with many different nested items. I am wondering what your thoughts are on flattening the QuestionnaireResponse items to increase efficiency. The linkIds (which we have made to be fairly descriptive) should provide us enough context for our use-case. I am wondering if there are any other caveats to flattening items in the QuestionnaireResponse. Thanks for your help!
r
Hi @kwillard95 , we actually have a helper function to do that! https://www.medplum.com/docs/sdk/modules#getquestionnaireanswers
j
How does this handle situations like the below, where we have multiple items that are grouped in an answer:
Copy code
{
  "resourceType": "QuestionnaireResponse",
  "status": "completed",
  "item": [
    {
      "linkId": "med-section",
      "text": "Medications",
      "item": [
        {
          "linkId": "med-info",
          "text": "Medication Information",
          "answer": [ // The list of medication info is a grouped set of answers
            {
              "item": [
                {
                  "linkId": "med-name",
                  "text": "Medication Name",
                  "answer": [
                    {
                      "valueString": "Ibuprofen"
                    }
                  ]
                },
                ... // Repeated for all medication values
              ]
            }
          ]
        },
        ... // Repeated for each medication
      ]
    }
  ]
}
The thing I'm wondering about specifically is how flattened lists deal with nested sets of answers within a higher level answer
And what exactly is an appropriate time to directly put something within
answer.item
versus just nesting another
item
r
1. Looking at the implmentation, this would only return the first answer in the list :/. But it would be pretty easy to update our helper to return an array if multiple answers are listed for an item https://github.com/medplum/medplum/blob/main/packages/core/src/utils.ts#L285 2. Best practice is for the structure of QuestionnaireReponse.item to mirror the corresponding Questionnaire.item structure. The FHIR spec has this note (https://www.medplum.com/docs/api/fhir/resources/questionnaireresponse#properties). The guidance is really that you can nest questions inside other questions when they have some sort of dependency on the parent question. These two examples ([quesitonnaire](http://hl7.org/fhir/R4/questionnaire-example.json); [questionnaireResponse](http://hl7.org/fhir/R4/questionnaireresponse-example.json) are illustrative
167 Views