How to build JSON with nested lists

I need to dynamically build the next JSON.

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

I tried it with the jsonlite package, but I cannot build an internal JSON object. Please help me try to resolve this.

+4
source share
1 answer

As stated 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 result:

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

Source: https://habr.com/ru/post/1671425/


All Articles