I'm currently working on a project where I need to convert a nested JSON structure into a DataFrame using R. I'm facing some issues with the current approach, and I'd appreciate any help or guidance on how to properly handle this conversion.
Json file looks like this :
json_data <- '{
"resourceType": "QuestionnaireResponse",
"id": "example-questionnaireresponse",
"questionnaire": "Questionnaire/example",
"status": "completed",
"subject": {
"reference": "Patient/example"
},
"authored": "2023-12-19T12:00:00Z",
"source": {
"reference": "Patient/example"
},
"item": [
{
"linkId": "page1",
"text": "Page 1",
"item": [
{
"linkId": "1.1",
"text": "Quel est votre nom?",
"answer": [
{
"valueString": "Participant 1"
},
{
"valueString": "Participant 2"
}
]
},
{
"linkId": "1.2",
"text": "Quel est votre âge?",
"answer": [
{
"valueInteger": 30
},
{
"valueInteger": 25
}
]
},
{
"linkId": "1.3",
"text": "Quel est votre lieu de résidence?",
"answer": [
{
"valueString": "Ville A"
},
{
"valueString": "Ville B"
}
]
}
]
},
{
"linkId": "page2",
"text": "Page 2",
"item": [
{
"linkId": "2.2",
"text": "Nombre dannées dexpérience?",
"answer": [
{
"valueInteger": 5
},
{
"valueInteger": 2
}
]
},
{
"linkId": "2.3",
"text": "ĂŠtes-vous heureux?",
"answer": [
{
"valueBoolean": true
},
{
"valueBoolean": false
}
]
}
]
}
]
}'
The json file is the response of two partipants to a questionnaire! I'd like a dataframe where each row contains the responses of a participants while a column contains the responses to a specific question.
my approach :
df <- fromJSON(json_data) %>%
as_tibble() %>%
unnest_wider(item) %>%
unnest_wider(item) %>%
unnest_wider(answer) %>%
select(linkId, text, valueString, valueInteger, valueBoolean)
which obviosuly doesnt work ..
How can I convert a FHIR json file structure to dataframe? an unnested dataframe at that.