I have post this problem on the github homepage of jsonlite, no repsonse currently, https://github.com/jeroen/jsonlite/issues/390
Other package available to obtain the result?
MWE:
library(jsonlite)
ifsxls <- structure(list(full = c("NATURE REVIEWS DRUG DISCOVERY", "LANCET"
), ab = c("NAT REV DRUG DISCOV", "LANCET"), ifc = c("84.694",
"79.321"), if5 = c("80.543", "77.237")), row.names = c(NA, -2L
), class = c("tbl_df", "tbl", "data.frame"))
toJSON(ifsxls, pretty=TRUE)
Now:
[
{
"full": "NATURE REVIEWS DRUG DISCOVERY",
"ab": "NAT REV DRUG DISCOV",
"ifc": "84.694",
"if5": "80.543"
},
{
"full": "LANCET",
"ab": "LANCET",
"ifc": "79.321",
"if5": "77.237"
}
]
Expect:
{
"NATURE REVIEWS DRUG DISCOVERY" : ["NAT REV DRUG DISCOV", "84.694", "80.543"],
"LANCET" : ["LANCET", "79.321", "77.237"]
}
Many thanks.
CodePudding user response:
That is a very unusal desired output but you can transform your data.frame into a shape that would better correspond to your desired JSON shape. For example, using base R functions, you can do
ifsxls |>
split(~full) |>
lapply(function(x) unlist(subset(x, select=-full))) |>
jsonlite::toJSON(pretty=TRUE)
# {
# "LANCET":["LANCET","79.321","77.237"],
# "NATURE REVIEWS DRUG DISCOVERY":["NAT REV DRUG DISCOV","84.694","80.543"]
# }