R create JSON in R

2.3k views Asked by At

I want to get to get the values from this JSON string: { "item": [ { "value": 142 } , { "value": 200 } ] }

R code:

library(rjson)
item <- list(item=c(value= 142,value=200))
toJSON(item)

And I have:

> toJSON(item)
[1] "{\"item\":{\"value\":142,\"value\":200}}"

How do I get the values from the json string in R?

1

There are 1 answers

0
cory On

Discussion of I() and what it does is in the RJSONIO documentation, but that's what you are looking for...

library("RJSONIO")
item <- list(item=I(list(list(value= 142),list(value=200))))
> toJSON(item)
[1] "{\n \"item\": [\n {\n \"value\":    142 \n},\n{\n \"value\":    200 \n} \n] \n}"