Andrii
Andrii

Reputation: 3043

How remove backslashes in response for jsonlite in R?

I have the following code for R Plumber API server

library(jsonlite)
library(data.table)

#' Home endpoint
#' @get /
function(){
  df <- data.table(msg = "Welcome")
  toJSON(df)
}

It gives me ["[{\"msg\":\"Welcome\"}]"] result on API.

How to replace \" on " symbol to make it more human-friendly when working in a browser or Postman? Expected result is "msg":"Welcome".

Thanks!

Upvotes: 1

Views: 481

Answers (1)

r2evans
r2evans

Reputation: 160607

plumber already jsonifies it, you are doubling it. Try this:

#' Home endpoint
#' @get /
function(){
  df <- data.table::data.table(msg = "Welcome")
  return(df)
}

And then in my console, I ran:

pr <- plumber::plumb("~/StackOverflow/4393334/60918243.R")
pr$run()
# Starting server to listen on port 5225
# Running the swagger UI at http://127.0.0.1:5225/__swagger__/

And then on my bash shell:

$ curl -s localhost:5225
[{"msg":"Welcome"}]

Upvotes: 1

Related Questions