0

I am trying to convert list of data to map data with keys and values. But iam facing the problem to convert it, I have tried with the below code but i is not satisfying to my requirement. I need the data as show in the image below

> df <- data.frame( sec = c('15:31:36',"15:31:37",'15:31:37','15:31:37','15:31:38','15:31:39'), 
                   label = c("Choose to and fro flights","Choose to and fro flights","Details","Details","Details","Details"), 
                   responseCode = c(200,200,200,'Non HTTP response code: org.apache.http.NoHttpResponseException','200','200'), 
                   Counting = c(9,1,2,2,5,1))

> purList1 <- lapply(split(df, df$label), function(x) split(x, x$responseCode,drop = T))

> output <- toJSON(
           list( ResponseCode = 
          lapply(names(purList1),function(x){
                ss = purList1[[x]]
                tt = lapply(names(ss),function(y){                       
                   items = ss[[y]][c("sec","Counting")]
                   sub = data.frame( R_C_seconds = as.vector(items$sec),
                                       R_C_Count = as.vector(items$Counting)
                   )

                  c(as.vector(unlist(data.frame(y))), as.vector(sub))
               }) 

              c(as.vector(x),  as.vector(tt))
           })
 )
 ,pretty = T)

Genereated Optput for the above code:

{
  "ResponseCode": [
    [
      ["Choose to and fro flights"],
      {
        "1": ["200"],
        "R_C_seconds": ["15:31:36", "15:31:37"],
        "R_C_Count": [9, 1]
      }
    ],
    [
      ["Details"],
      {
        "1": ["200"],
        "R_C_seconds": ["15:31:37", "15:31:38", "15:31:39"],
        "R_C_Count": [2, 5, 1]
      },
      {
        "1": ["Non HTTP response code: org.apache.http.NoHttpResponseException"],
        "R_C_seconds": ["15:31:37"],
        "R_C_Count": [2]
      }
    ]
  ]
} 

but Iam excepting the json output in the below specified format key value pairs.

enter image description here

dondapati
  • 829
  • 6
  • 18

1 Answers1

1

Your code seems unnecessarily complicated. One can easily get confused by the tree of lists. Please do not try handling whole purList1 at once; play with the subsets like purList1[[1]] first.


reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-11-18

df <- data.frame( sec = c('15:31:36',"15:31:37",'15:31:37','15:31:37','15:31:38','15:31:39'), 
                  label = c("Choose to and fro flights","Choose to and fro flights","Details","Details","Details","Details"), 
                  responseCode = c(200,200,200,'Non HTTP response code: org.apache.http.NoHttpResponseException','200','200'), 
                  Counting = c(9,1,2,2,5,1))

purList1 <- lapply(split(df, df$label), function(x) split(x, x$responseCode,drop = T))


# play with a subset and construct the function to apply
jsonlite::toJSON(
  lapply(
    purList1[[1]],
    function(x) list(
      R_C_seconds = x$sec,
      R_C_Count   = x$Counting
    )
  ),
  pretty = TRUE
)
#> {
#>   "200": {
#>     "R_C_seconds": ["15:31:36", "15:31:37"],
#>     "R_C_Count": [9, 1]
#>   }
#> }


# apply the function on the whole list
jsonlite::toJSON(
  lapply(
    purList1,
    lapply,
    function(x) list(
      R_C_seconds = x$sec,
      R_C_Count   = x$Counting
    )
  ),
  pretty = TRUE
)
#> {
#>   "Choose to and fro flights": {
#>     "200": {
#>       "R_C_seconds": ["15:31:36", "15:31:37"],
#>       "R_C_Count": [9, 1]
#>     }
#>   },
#>   "Details": {
#>     "200": {
#>       "R_C_seconds": ["15:31:37", "15:31:38", "15:31:39"],
#>       "R_C_Count": [2, 5, 1]
#>     },
#>     "Non HTTP response code: org.apache.http.NoHttpResponseException": {
#>       "R_C_seconds": ["15:31:37"],
#>       "R_C_Count": [2]
#>     }
#>   }
#> }

(I'm not sure but jsonlite::toJSON(purList1, dataframe = "column", pretty = TRUE) may be enough if you just want to convert data.frames in column-direction)

yutannihilation
  • 798
  • 4
  • 9
  • Thank you @yutannihilation. – dondapati Nov 18 '17 at 16:27
  • Initially i am trying the `jsonlite::toJSON(purList1, dataframe = "column", pretty = TRUE)` ,But not using the parameter `dataframe` So i am writing the lenghty code. And if i am using the above code `jsonlite::toJSON(purList1, dataframe = "column", pretty = TRUE)` i get the all columns but i need only `sec,Counting` columns so I prefer to use the above code. Once again thank you. – dondapati Nov 20 '17 at 05:31
  • I see, it's reasonable to be lengthy. Thanks for the detail :) – yutannihilation Nov 20 '17 at 12:54