0

I am sending a http request where the body needs to be in json format. As standard it seems that value null is being quoted. This prevents the http request to work properly. When testing to build a string manually and removing the quotes for value null, then the http request works fine.

Question:

Can jsonlite handle to strip off the quotes from all null values?

My current code:

library(jsonlite)

x <- list(epic = "Stockholm", currency = "null")
json <- toJSON(x,  auto_unbox = TRUE)

Gives result, that does not work:

{"epic":"Stockholm","currency":"null"} 

This manually constructed string works:

{"epic":"Stockholm","currency": null} 
Toolbox
  • 2,333
  • 12
  • 26
  • 2
    Your original `x` doesn't have `NULL`, it has a character string `"null"`. The `jsonlite` arguments for interpreting null-like objects is the `null=` argument in `jsonlite::toJSON`. If your input data has strings of `"null"`, then you have a problem with generating the input data, not with `jsonlite`. – r2evans Jul 10 '19 at 20:43

2 Answers2

3

As seen in the docs, you might need to use NA instead of "null" :

library(jsonlite)

x <- list(epic = "Stockholm", currency = NA)
json <- toJSON(x,  auto_unbox = TRUE, na = "null")
0

Below works and solves the problem.

It seems my source data need to specify value NULL instead of "null", together with the setting of how to encode value null.

library(jsonlite)

x <- list(epic = "Stockholm", currency = NULL)
json <- toJSON(x,  auto_unbox = TRUE, null = "null")
Toolbox
  • 2,333
  • 12
  • 26