When generating a JSON object from a data frame in R, "in" is converted to "in." and I'm not sure how to solve this. Any help would be appreciated!
library(jsonlite)
query_json3 <- data.frame("eClass" = c("Fermentation","Sample", "ResultValue","Experiment"),
"collection" = c("fermentations","samples", "resultValues", "experiments"))
filters1 <- data.frame("field" = "attributes.experiment",
"value" = "EXP-EB-22-019")
filters2 <- data.frame("field" = "originID",
"in" = "fermentations.originID")
filters3 <- data.frame("field" = "SubjectID",
"in" = "samples.id")
filters4 <- data.frame("field" = "type",
"value" = "Small-Scale Screening")
query_json3[1, "filters"][[1]] <- list(filters1)
query_json3[2, "filters"][[1]] <- list(filters2)
query_json3[3, "filters"][[1]] <- list(filters3)
query_json3[4, "filters"][[1]] <- list(filters4)
toJSON(query_json3)
Output:
[{"eClass":"Fermentation","collection":"fermentations","filters":[{"field":"attributes.experiment","value":"EXP-EB-22-019"}]},{"eClass":"Sample","collection":"samples","filters":[{"field":"originID","in.":"fermentations.originID"}]},{"eClass":"ResultValue","collection":"resultValues","filters":[{"field":"SubjectID","in.":"samples.id"}]},{"eClass":"Experiment","collection":"experiments","filters":[{"field":"type","value":"Small-Scale Screening"}]}]
Desired output:
[{"eClass":"Fermentation","collection":"fermentations","filters":[{"field":"attributes.experiment","value":"EXP-EB-22-019"}]},{"eClass":"Sample","collection":"samples","filters":[{"field":"originID","in":"fermentations.originID"}]},{"eClass":"ResultValue","collection":"resultValues","filters":[{"field":"SubjectID","in":"samples.id"}]},{"eClass":"Experiment","collection":"experiments","filters":[{"field":"type","value":"Small-Scale Screening"}]}]
The smoking gun is not jsonlite here. in
is a reserved keyword in R, and for this reason, data.frame
adds the period. You can use check.names=FALSE
:
data.frame("field" = "SubjectID",
"in" = "samples.id")
# field in.
# 1 SubjectID samples.id
data.frame("field" = "SubjectID",
"in" = "samples.id",
check.names = FALSE)
# field in
# 1 SubjectID samples.id