Home > Mobile >  Unwanteed period added to "in" when making a JSON from data frame (R jsonlite)
Unwanteed period added to "in" when making a JSON from data frame (R jsonlite)

Time:07-13

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"}]}] 

CodePudding user response:

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

CodePudding user response:

The problem is not actually to do with jsonlite. If you look at your dataframe filters3 then you can see that the column name has changed already there so jsonlite is doing exactly what it should. There are reserved words in R (see for example here: https://learnetutorials.com/r-programming/identifiers-constants-reserved-words) and "in" is a reserved word. Can you change the name of this field without breaking things elsewhere? If the name is causing problems here then it is likely to cause problems elsewhere too.

  • Related