【问题标题】:Converting a dataframe to nested json format将数据框转换为嵌套的 json 格式
【发布时间】:2016-09-21 18:56:17
【问题描述】:

我正在处理一个如下所示的 r 数据框。

       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}
                        ]
                      }
                ]
  }
]'

我不确定如何完成此操作,因此非常感谢任何有关将数据集转换为这种格式的帮助。谢谢。

【问题讨论】:

    标签: json r jsonlite


    【解决方案1】:

    一种生硬的方法不是很优雅,但可能适合您的用例:

    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
                        }
                    ]
                }
            ]
        }
    ]
    

    【讨论】:

    • @jdharison,太完美了。谢谢 JDHarisson
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2019-07-08
    • 2021-12-17
    • 1970-01-01
    • 2017-03-21
    • 2020-12-16
    相关资源
    最近更新 更多