1

I want to send json format data for a url request. My code is as follows with num as an input;

#* @get /getComm
getComm <- function(num=1){
library(jsonlite) 
#some computation here
lst<-list(links=linksff,nodes=sc,directed=FALSE,multigraph=FALSE)
return(toJSON(lst))
}

And I use plumber library to make my code as an API. the lst list is like below for num=1;

$links
  source target
1      0      3
2      2      5
3      1      4

$nodes
  size score  id   type
1   10    10   7 circle
2   10    10 179 circle
3   10    10 128 circle
4   10    10 191 circle
5   10    10 239 circle
6   10    10 218 circle

$directed
[1] FALSE

$multigraph
[1] FALSE

When I convert it to json by toJSON(lst) the json format is correct:

{"links":[{"source":0,"target":3},{"source":2,"target":5},{"source":1,"target":4}],"nodes":[{"size":10,"score":10,"id":7,"type":"circle"},{"size":10,"score":10,"id":179,"type":"circle"},{"size":10,"score":10,"id":128,"type":"circle"},{"size":10,"score":10,"id":191,"type":"circle"},{"size":10,"score":10,"id":239,"type":"circle"},{"size":10,"score":10,"id":218,"type":"circle"}],"directed":[false],"multigraph":[false]}

however, when I send url request to get json, the browser cannot correctly diagnose json format and the are extra slashe_which I know means space in R. the response for the url request http://127.0.0.1:8000/getComm?num=1 is like below;

["{\"links\":[{\"source\":0,\"target\":3},{\"source\":2,\"target\":5},{\"source\":1,\"target\":4}],\"nodes\":[{\"size\":10,\"score\":10,\"id\":7,\"type\":\"circle\"},{\"size\":10,\"score\":10,\"id\":179,\"type\":\"circle\"},{\"size\":10,\"score\":10,\"id\":128,\"type\":\"circle\"},{\"size\":10,\"score\":10,\"id\":191,\"type\":\"circle\"},{\"size\":10,\"score\":10,\"id\":239,\"type\":\"circle\"},{\"size\":10,\"score\":10,\"id\":218,\"type\":\"circle\"}],\"directed\":[false],\"multigraph\":[false]}"]

where do these slashes come from?

Zahra Aminolroaya
  • 520
  • 1
  • 5
  • 28

1 Answers1

0

plumber associates every endpoint with a "serializer" -- a concept that isn't well documented today -- with the default serializer being JSON.

@effel is correct that the backslashes are escaping the quotes. What you're looking at in the response is a single string that contains the result of your toJSON. You've effectively double-encoded your object -- first using your own toJSON call to get a string, then again plumber will encode that string into JSON, resulting in the backslashes.

I suspect you actually just want to return the object, not the JSON serialization and you'll get the right answer.

#* @get /getComm
getComm <- function(num=1){
  #some computation here
  lst<-list(links=linksff,nodes=sc,directed=FALSE,multigraph=FALSE)
  return(lst)
}

If you really do need to do your own custom JSON serialization for whatever reason, see this answer on how to tell plumber not to serialize your output. https://stackoverflow.com/a/44092595/1133019

Community
  • 1
  • 1
Jeff Allen
  • 17,277
  • 8
  • 49
  • 70