I want to convert the below dataframe into JSON but I am not sure how to achieve it. I tried to find some help online but looks like no one has done it on a r dataframe.
df<-data.frame(profileKey=c(7,7,7,8,8,8),joinDate=c('2007-11-01','2007-11-
01','2007-11-01','1993-01-04','1993-01-04','1993-01-04'),Year=c('2013','2014','2015','2013','2014','2015'),monthlySalary=c('2000','3251','2015','4355','1112','33223'),event=c(0,0,0,0,0,1))
My desired output is :
{
"7": {
"Profile_key": 7,
"Join.Date": "2007-11-01",
"event": {
"2013": "0",
"2014": "0",
"2015": "0"
},
"monthly_salary": {
"2013": 2000,
"2014": 3251,
"2015": 2015
}
},
"8": {
"Profile_key": 8,
"Join.Date": "1993-01-04",
"event": {
"2013": "0",
"2014": "0",
"2015": "1"
},
"monthly_salary": {
"2013": 4355,
"2014": 1112,
"2015": 33223
}
}
}
NOTE: There is a similar question convert data frame to json but it is not an exact duplicate. My question addresses the issue of having nested jsons , basically how to generate data in form of time series for some variables . This needs additional data wrangling rather than just feeding it into
toJson()
function.
Any help is appreciated.