r - how do you convert df to json file with numbers as number -
i trying convert data frame json format. data frame called x:
structure(list(desc = c("web", "mobile", "tv", "store"), total = c(223786915, 42053151, 232299534, 26317530), name = c("total login", "total login", "total login", "total login")), .names = c("desc", "total", "name" ), row.names = c(na, 4l), class = "data.frame")
this function:
servers <- split(x,x$name) dumfun <- function(x){ sdata <- servers[x][[1]] if(nrow(sdata) >0){ # create appropriate list dumlist <- unname(apply(sdata[,c(1,as.numeric(2))], 1, function(y) unname(as.list(y)))) return(tojson(list(name = x, data = dumlist))) } } jsdata <- lapply(names(servers), dumfun) jsind <- sapply(jsdata, is.null) p<-paste(jsdata[!jsind], collapse = ',') p<-paste('[', p, ']')
it treating x$total string. need x$total number , don't have double quotes around it. ideas might doing wrong?
does jsonlite
want?
for example can jsonlite::tojson(x)
res <- jsonlite::tojson(x, pretty=true) cat(res) [ { "desc" : "web", "total" : 223786915, "name" : "total login" }, { "desc" : "mobile", "total" : 42053151, "name" : "total login" }, { "desc" : "tv", "total" : 232299534, "name" : "total login" }, { "desc" : "store", "total" : 26317530, "name" : "total login" } ]
Comments
Post a Comment