Toolbox
Toolbox

Reputation: 2491

r converted to json, need value null to be unquoted

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} 

Upvotes: 0

Views: 1655

Answers (2)

Toolbox
Toolbox

Reputation: 2491

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")

Upvotes: 0

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")

Upvotes: 3

Related Questions