I would like to convert a dataframe into a nested json object, and determine where to create a nested json object based on the column names.
I have made a toy example to explain the problem. Given this dataframe:
df <- read.csv(textConnection(
"id,name,allergies.pollen,allergies.pet,attributes.height,attributes.gender
x,alice,no,yes,175,female
y,bob,yes,yes,180,male"))
Or in a more readable format:
id name allergies.pollen allergies.pet attributes.height attributes.gender
1 x alice no yes 175 female
2 y bob yes yes 180 male
Then I would like the following json object:
'[
{
"id": "x",
"name": "alice",
"allergies":
{
"pollen": "no",
"pet": "yes"
},
"attributes":
{
"height": "175",
"gender": "female"
}
},
{
"id": "y",
"name": "bob",
"allergies":
{
"pollen": "yes",
"pet": "yes"
},
"attributes":
{
"height": "180",
"gender": "male"
}
}
]'
So it should automatically group the columns at a fixed separator ".".
Ideally it should be able to handle nested-nested objects too, e.g. allergies.pet.cat
and allergies.pet.dog
.
My best idea on solving this problem is making a function, which recursive call jsonlite::toJSON
and extracts the categories using stringr::str_extract("^[^.]*")
, but I have not been able to make this work.