6

I need to dynamically construct the following JSON.

{
  "status": "SUCCESS",
  "code": "200",
  "output": {
    "studentid": "1001",
    "name": "Kevin"
  }
}

I tried it with the jsonlite package, but I can't construct the inner JSON object. Please help me try to resolve this.

Rich Scriven
  • 90,041
  • 10
  • 148
  • 213
Rajesh Kumar
  • 611
  • 6
  • 19

1 Answers1

10

As mentioned in the comments, you can create a nested list and use toJSON().

library(jsonlite)
x <- list(status = "SUCCESS", code = "200", 
    output = list(studentid = "1001", name = "Kevin"))
toJSON(x, pretty = TRUE, auto_unbox = TRUE)

which gives the following output:

{
  "status": "SUCCESS",
  "code": "200",
  "output": {
    "studentid": "1001",
    "name": "Kevin"
  }
} 
Rich Scriven
  • 90,041
  • 10
  • 148
  • 213