1

I am using OpenCPU and R to create a web API that takes in some inputs and returns a topoJSON file from a database, as well as some other information. OpenCPU automatically pushes the output through toJSON, which results in JSON output that has quoted JSON in it (i.e., the topoJSON). This is obviously not ideal--especially since it then gets incredibly cluttered with backticked quotes (\"). I tried using fromJSON to convert it to an R object, which could then be converted back (which is incredibly inefficient), but it returns a slightly different syntax and the result is that it doesn't work.

I feel like there should be some way to convert the string to some other type of object that results in toJSON calling a different handler that tells it to just leave it alone, but I can't figure out how to do that.

> s <- '{"type":"Topology","objects":{"map": "0"}}'
> fromJSON(s)
$type
[1] "Topology"

$objects
$objects$map
[1] "0"

> toJSON(fromJSON(s))
{"type":["Topology"],"objects":{"map":["0"]}} 

That's just the beginning of the file (I replaced the actual map with "0"), and as you can see, brackets appeared around "Topology" and "0". Alternately, if I just keep it as a string, I end up with this mess:

> toJSON(s)
["{\"type\":\"Topology\",\"objects\":{\"0000595ab81ec4f34__csv\": \"0\"}}"] 

Is there any way to fix this so that I just get the verbatim string but without quotes and backticks?

EDIT: Note that because I'm using OpenCPU, the output needs to come from toJSON (so no other function can be used, unfortunately), and I can't do any post-processing.

Caroline
  • 450
  • 1
  • 5
  • 15

2 Answers2

2

To it seems you just want the values rather than vectors. Set auto_unbox=TRUE to turn length-one vectors into scalar values

toJSON(fromJSON(s), auto_unbox = TRUE)
# {"type":"Topology","objects":{"map":"0"}} 

That does print without escaping for me (using jsonlite_1.5). Maybe you are using an older version of jsonlite. You can also get around that by using cat() to print the result. You won't see the slashes when you do that.

cat(toJSON(fromJSON(s), auto_unbox = TRUE))
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Sorry, should have clarified what OpenCPU is, since it doesn't seem to be used that much (although it's quite useful). Basically the way it works is that you create an R function and are able to use it as a web API service. It will return JSON, which it converts automatically using jsonlite. There are some annoying limitations, like that you can't just have it return whatever string you want. If I could just have it return a string, it wouldn't be a problem--I could just put markers at the beginning and end. Unfortunately it doesn't work that way. – Caroline Jul 18 '17 at 13:51
  • Then I suggest you edit your question to more clearly demonstrate the problem. – MrFlick Jul 18 '17 at 13:53
1

You can manually unbox the relevant entries:

library(jsonlite)
s <- '{"type":"Topology","objects":{"map": "0"}}'
j <- fromJSON(s)
j$type <- unbox(j$type)
j$objects$map <- unbox(j$objects$map)
toJSON(j)
# {"type":"Topology","objects":{"map":"0"}} 
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75