0

I am looking to create a valid JSON string, with input from an R list. Each key and value needs to be quotation escaped (see expected valid JSON). I use jsonlite package for transform to JSON.

The result string is used for an API that requires the quotation escapes.

R-list:

myProducts <- list(
                  productOne = "x",
                  productTwo = "y"
)

Convert list to json format:

myJSON <- toJSON(myProducts)

Output:

{"productOne":["x"],"productTwo":["y"]} 

Expected valid json / Neeed string:

"{\"productOne\": \"x\", \"productTwo\": \"y\"}"

Toolbox
  • 2,333
  • 12
  • 26

1 Answers1

0

Here is how you can get the expected string:

> as.character(toJSON(myProducts, auto_unbox = TRUE))
[1] "{\"productOne\":\"x\",\"productTwo\":\"y\"}"
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225