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?