1

Need Help, I just create API with R Plumber, I just would like the Result Json will be like this

enter image description here

Instead of like this

enter image description here

below are my R Plumber script

library(plumber)
library(jsonlite)


#* @get /nested_json
#* @serializer unboxedJSON
function() {
    
  main_info <- data.frame(
    Name = "John",
    Address = "Stret No. 1",
    Point = "600"
  )
  
  reason_info <- data.frame(code = c('AAA', 'BBB', 'CCC', NA, 'EEE'),
                            descriptiom = c('low value of point A', 'low value of point B', 'low value of point C', NA, 'low value of point D')  )
  
  main_info[1, "reason"][[1]] <- list(reason_info)
  
  final_output <- as.list(main_info)
  final_output
  
}

Thanks a lot for any kind of suggestion

UPDATE

The alternative solution from @deschen just solve the problem, my teammates that created the Web Application could use this solution.

the alternative solution are like below :

  {
  "Name": "John",
  "Address": "Stret No. 1",
  "Point": "600",
  "reason": [
    {
      "code": "AAA",
      "description": "low value of point A"
    },
    {
      "code": "BBB",
      "description": "low value of point B"
    },
    {
      "code": "CCC",
      "description": "low value of point C"
    },
    {
      "code": null,
      "description": null
    },
    {
      "code": "EEE",
      "description": "low value of point D"
    }
  ]
} 
Reza AM
  • 73
  • 6
  • I don't have an immediate solution, but I have trialed & errored with such API structures in that past a bit. So in your case, it's all about how to combine your main and reason info into lists of lists. I'm providing some code as an answer, although it's not working as you want, but maybe it gives you some hints and ideas on how to proceed. – deschen Sep 16 '21 at 11:25
  • Thanks a lot for the solution, I just add drop_na() to remove the specific rows that has NA – Reza AM Sep 16 '21 at 15:15
  • Curious, is there any reason why you want the JSON code to look different thatn in this API documentation? I mean if it should serve the same purpose, then the code should look like what the documentation specifies, no? – deschen Sep 16 '21 at 21:40
  • so I created some calculation engine in R...and will be called by API from a web application (User Interface) that my friends build...my friends would like the Json format as shown at above expectation...and now he already confirms that will use the format from your solution – Reza AM Sep 17 '21 at 02:04
  • So the code I provided does solve your problem? If so you might wsnt to accept this answer and maybe also update your question so that it‘s clesr that this alternative solution is acceptable. – deschen Sep 17 '21 at 04:36

1 Answers1

1
library(tidyverse)
library(jsonlite)

reason_info <- data.frame(code        = c('AAA', 'BBB', 'CCC', NA, 'EEE'),
                          description = c('low value of point A',
                                          'low value of point B',
                                          'low value of point C',
                                           NA,
                                          'low value of point D'))

reason_info_new <- reason_info %>%
  mutate(new = apply(across(everything()), 1, as.list)) %>%
  pull(new)

full <- list(Name    = "John",
             Address = "Stret No. 1",
             Point   = "600",
             reason = reason_info_new)

myJSON <- toJSON(full, pretty = TRUE, auto_unbox = TRUE)
myJSON

which gives:

{
  "Name": "John",
  "Address": "Stret No. 1",
  "Point": "600",
  "reason": [
    {
      "code": "AAA",
      "description": "low value of point A"
    },
    {
      "code": "BBB",
      "description": "low value of point B"
    },
    {
      "code": "CCC",
      "description": "low value of point C"
    },
    {
      "code": null,
      "description": null
    },
    {
      "code": "EEE",
      "description": "low value of point D"
    }
  ]
} 
deschen
  • 10,012
  • 3
  • 27
  • 50