How can I encode an R vector of length 1 as a single value in json using the jsonlite R package?

后端 未结 1 1448
不思量自难忘°
不思量自难忘° 2021-01-05 17:01

I am trying to encode R lists into json using the jsonlite package and the toJSON function. I have a simple item like:

list(op=\'abc\')

I\'

相关标签:
1条回答
  • 2021-01-05 17:26

    The auto_unbox argument does the trick with the jsonlite package:

    toJSON(list(op='abc'),auto_unbox=TRUE)
    

    yields:

    {"op":"abc"}
    

    Update: based on comment, this approach is probably safer, and an example of why:

    > jsonlite::toJSON(list(x=unbox(1),y=c(1,2)))
    {"x":1,"y":[1,2]} 
    > jsonlite::toJSON(list(x=unbox(1),y=unbox(c(1,2)))) # expect error here.
    Error: Tried to unbox a vector of length 2
    
    0 讨论(0)
提交回复
热议问题