Search code examples
rjsonreplacejsonlite

How to create a json with R and controll quotes


I want to create data in R and then export as json. For this I create a list and transform it with jsonlite:: toJSON. In the json I get all characters in quotes. How can I remove specific quotes. as.name() and noquote() does not work with jsonlite:: toJason. Is there a solution to get this done.

Here an example:


x = list(test="false")

x_j=jsonlite::toJSON( x,pretty = T,  auto_unbox = T)
{
  "test": "false"
}

However, the export should look like this:


{
  "test": false
}

Solution

  • jsonlite::toJSON will only convert an R logical vector to a json boolean.

    In your case, that means you need FALSE, rather than "false", which is an R character vector and gets converted to a json string.

    x = list(test=FALSE)
    
    jsonlite::toJSON(x, pretty = T,  auto_unbox = T)
    
    # {
    #   "test": false
    # }