Skip to content

Instantly share code, notes, and snippets.

@jamesthomson jamesthomson/nested df to josn.R Secret
Last active Aug 29, 2015

Embed
What would you like to do?
nested data frame to json
library(rjson)
jsonNestedData<-function(structure, values=NULL, top_label="Top") {
if (is.null(values)) {
#bottom level
labels<-data.frame(table(data[,ncol(data)-1]))
for (i in c(1:nrow(labels))) {
items<-data[data[,ncol(data)-1]==labels[i,1],ncol(data)]
eval(parse(text=paste0(gsub(" ", "_",labels[i,1]),"<-list(name=\"", labels[i,1], "\", children=list(", paste0("list(name=as.character(items[", c(1:length(items)), "]))", collapse=","), "))")))
}
#iterate through other levels
for (c in c((ncol(data)-2):1)) {
labels<-data.frame(table(data[,c]))
lookup<-data.frame(table(data[,c], data[,c+1]))
lookup2<-lookup[lookup$Freq!=0,]
for (i in c(1:nrow(labels))) {
eval(parse(text=paste0(gsub(" ", "_",labels[i,1]),
"<-list(name=\"",
labels[i,1],
paste0("\", children=list(",
paste(gsub(" ", "_", lookup2[lookup2$Var1==labels[i,1],2]), collapse=","), ")"),
")")
))
}
}
#final top level
labels<-data.frame(table(data[,1]))
eval(parse(text=paste0("Top<-list(name=\"", top_label,"\" , children=list(", paste(gsub(" ", "_", labels[,1]), collapse=","), ")",")")))
} else {
#bottom level
labels<-data.frame(table(structure[,ncol(structure)-1]))
for (i in c(1:nrow(labels))) {
items<-structure[structure[,ncol(structure)-1]==labels[i,1],ncol(structure)]
vals<-values[structure[,ncol(structure)-1]==labels[i,1]]
eval(parse(text=paste0(gsub(" ", "_",labels[i,1]),"<-list(name=\"", labels[i,1], "\", children=list(", paste0("list(name=as.character(items[", c(1:length(items)), "]), value=vals[",c(1:length(items)),"])", collapse=","), "))")))
}
#iterate through other levels
for (c in c((ncol(structure)-2):1)) {
labels<-data.frame(table(structure[,c]))
lookup<-data.frame(table(structure[,c], structure[,c+1]))
lookup2<-lookup[lookup$Freq!=0,]
for (i in c(1:nrow(labels))) {
eval(parse(text=paste0(gsub(" ", "_",labels[i,1]),
"<-list(name=\"",
labels[i,1],
paste0("\", children=list(",
paste(gsub(" ", "_", lookup2[lookup2$Var1==labels[i,1],2]), collapse=","), ")"),
")")
))
}
}
#final top level
labels<-data.frame(table(structure[,1]))
eval(parse(text=paste0("Top<-list(name=\"", top_label,"\" , children=list(", paste(gsub(" ", "_", labels[,1]), collapse=","), ")",")")))
}
json<-toJSON(Top)
return(list(Type="json:nested", json=json))
}
structure<-data.frame(
Level1=c('Parent A','Parent A','Parent A','Parent A','Parent A','Parent A','Parent A','Parent A','Parent A','Parent A','Parent A','Parent A','Parent B','Parent B','Parent B','Parent B','Parent B','Parent B','Parent B','Parent C','Parent C','Parent C','Parent C','Parent C','Parent C'),
Level2=c('Child A','Child A','Child A','Child A','Child B','Child B','Child C','Child C','Child C','Child C','Child C','Child C','Child D','Child D','Child D','Child D','Child E','Child E','Child E','Child F','Child G','Child G','Child G','Child G','Child G'),
Level3=c('Sub Child 1','Sub Child 2','Sub Child 3','Sub Child 4','Sub Child 5','Sub Child 6','Sub Child 7','Sub Child 8','Sub Child 9','Sub Child 10','Sub Child 11','Sub Child 12','Sub Child 13','Sub Child 14','Sub Child 15','Sub Child 16','Sub Child 17','Sub Child 18','Sub Child 19','Sub Child 20','Sub Child 21','Sub Child 22','Sub Child 23','Sub Child 24','Sub Child 25')
)
values<-c(26:50)
JSON<-jsonNestedData(structure=structure, values=NULL, top_label="Top Level Label")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.