2016-09-21 78 views
3

我正在處理一個如下所示的r數據框。將數據框轉換爲嵌套json格式

 id date   items  price 
     10 2014/09/13  shoes  187 
     10 2014/09/13  belt   35 
     10 2014/09/14  shirt   69 
     12 2014/10/01  dress   58 
     12 2014/10/01  bag  118 
     12 2015/01/03  shoes  115 

purchase = structure(list(id = c(10, 10, 10, 12, 12, 12), date = structure(c(1L, 
1L, 2L, 3L, 3L, 4L), .Label = c("2014/09/13", "2014/09/14", "2014/10/01", 
"2015/01/03"), class = "factor"), name = structure(c(5L, 2L, 
4L, 3L, 1L, 5L), .Label = c("bag", "belt", "dress", "shirt", 
"shoes"), class = "factor"), price = c(187, 35, 69, 58, 118, 
115)), .Names = c("id", "date", "name", "price"), row.names = c(NA, 
-6L), class = "data.frame") 

我試圖將其轉換爲一個嵌套的JSON格式如下

[ 
    { 
    "id": "10", 
    "purchase": [ 
         { 
         "date": "2014/09/13", 
         "items": [ 
          {"id": "shoes", "price": 187}, 
          {"id": "belt", "price": 35} 
         ] 
         }, 
         { 
         "date": "2014/09/14", 
         "items": [ 
          {"id": "shirt", "price": 69} 
         ] 
         } 
       ] 
    }, 
    { 
    "id": "12", 
    "purchase": [ 
         { 
         "date": "2014/10/01", 
         "items": [ 
          {"id": "dress", "price": 58}, 
          {"id": "bag", "price": 118} 
         ] 
         }, 
         { 
         "date": "2015/01/03", 
         "items": [ 
          {"id": "shoes", "price": 115} 
         ] 
         } 
       ] 
    } 
]' 

我不知道如何在改造數據集這種格式是非常讚賞做到這一點,所以任何幫助。謝謝。

回答

2

鈍力的做法不是很優雅,但可能對你的使用情況做:

purchase = structure(list(id = c(10, 10, 10, 12, 12, 12), date = structure(c(1L, 
                      1L, 2L, 3L, 3L, 4L), .Label = c("2014/09/13", "2014/09/14", "2014/10/01", 
                              "2015/01/03"), class = "factor"), name = structure(c(5L, 2L, 
                                            4L, 3L, 1L, 5L), .Label = c("bag", "belt", "dress", "shirt", 
                                                   "shoes"), class = "factor"), price = c(187, 35, 69, 58, 118, 
                                                            115)), .Names = c("id", "date", "name", "price"), row.names = c(NA, 
                                                                            -6L), class = "data.frame") 

library(jsonlite) 
purList <- lapply(split(purchase, purchase$id), function(x) split(x, x$date)) 

newList <- lapply(names(purList), function(x){ 
    subList <- purList[[x]] 
    purchase <- lapply(names(subList), function(y){ 
    items <- subList[[y]][c("name", "price")] 
    if(nrow(items) > 0){ 
     names(items) <- c("id", "price") 
     list(date = y, items = items) 
    } 
    }) 
    list(id = x, purchase = purchase[!sapply(purchase, is.null)]) 
}) 
out <- toJSON(newList, auto_unbox = TRUE) 
prettify(out) 

> prettify(out) 
[ 
    { 
     "id": "10", 
     "purchase": [ 
      { 
       "date": "2014/09/13", 
       "items": [ 
        { 
         "id": "shoes", 
         "price": 187 
        }, 
        { 
         "id": "belt", 
         "price": 35 
        } 
       ] 
      }, 
      { 
       "date": "2014/09/14", 
       "items": [ 
        { 
         "id": "shirt", 
         "price": 69 
        } 
       ] 
      } 
     ] 
    }, 
    { 
     "id": "12", 
     "purchase": [ 
      { 
       "date": "2014/10/01", 
       "items": [ 
        { 
         "id": "dress", 
         "price": 58 
        }, 
        { 
         "id": "bag", 
         "price": 118 
        } 
       ] 
      }, 
      { 
       "date": "2015/01/03", 
       "items": [ 
        { 
         "id": "shoes", 
         "price": 115 
        } 
       ] 
      } 
     ] 
    } 
] 
+0

@jdharison,這是完美的。謝謝JDHarisson –