使用数组将 tibble/dataframe 转换为嵌套的 JSON

convert tibble/dataframe to nested JSON with arrays

如果有大量具有如下结构的小标题:

library(tidyverse)
library(jsonlite)

data <- tibble(Type = c(rep("A", 10), rep("B", 5)),
               SubType = c(rep("a", 2), rep("b", 3), rep("c", 3), rep("d", 2),
                           rep("e", 2), rep("f", 3)),
               SubTypeNr = c(rep(1, 2), rep(2, 3), rep(3, 3), rep(4, 2),
                           rep(5, 2), rep(6, 3)),
               SubTypeVal1 = c(seq(5, 41, by = 4), seq(2, 26, by = 5)),
               SubTypeVal2 = SubTypeVal1 + 1)

更新 按照 Nad Pads 的提示,我尝试了:

data %>% group_by(Type, SubType, SubTypeNr) %>%
  summarise(SubTypeVal1 = list(SubTypeVal1),
            SubTypeVal2 = list(SubTypeVal2)) %>%
  toJSON(pretty = TRUE)

这让我非常接近我想要的(见下文)。我必须如何对数据进行分组才能实现这样的效果(嵌套在类型中的子类型中的值):

{"Type" : "A", {
  "SubType" : "a",
  "SubTypeNr" : "1", {
    "SubTypeVal1" : [5, 9],
    "SubTypeVal1" : [6, 10],
  }
  "SubType" : "b",
  "SubTypeNr" : "2", {
    "SubTypeVal1" : [13, 17, 21],
    "SubTypeVal1" : [14, 18, 22],
  }
  "SubType" : "c",
  "SubTypeNr" : "3", {
    "SubTypeVal1" : [25, 29, 33],
    "SubTypeVal1" : [26, 30, 34],
  }
  "SubType" : "d",
  "SubTypeNr" : "4", {
    "SubTypeVal1" : [37, 41],
    "SubTypeVal1" : [38, 42],
  }
}
}
{"Type" : "B", {
  "SubType" : "e",
  "SubTypeNr" : "5", {
    "SubTypeVal1" : [2, 7],
    "SubTypeVal1" : [3, 8],
  }
  "SubType" : "f",
  "SubTypeNr" : "6", {
    "SubTypeVal1" : [12, 17, 22],
    "SubTypeVal1" : [13, 18, 23],
  }
}
}

data %>% 
  nest(TypeData = !Type) %>% 
  mutate(across(TypeData, map, nest, SubTypeData = !c(SubType, SubTypeNr))) %>% 
  mutate(across(TypeData, map, mutate, across(SubTypeData, map, summarise, across(everything(), list)))) %>%
  toJSON(pretty = TRUE)

或者,根据 OP 的更新:

data %>% 
  group_by(Type, SubType, SubTypeNr) %>%
  summarise(across(everything(), list), .groups = "drop") %>% 
  nest(SubTypeData = !c(Type, SubType, SubTypeNr)) %>% 
  nest(TypeData = !Type) %>% 
  toJSON(pretty = TRUE)
[
  {
    "Type": "A",
    "TypeData": [
      {
        "SubType": "a",
        "SubTypeNr": 1,
        "SubTypeData": [
          {
            "SubTypeVal1": [5, 9],
            "SubTypeVal2": [6, 10]
          }
        ]
      },
      {
        "SubType": "b",
        "SubTypeNr": 2,
        "SubTypeData": [
          {
            "SubTypeVal1": [13, 17, 21],
            "SubTypeVal2": [14, 18, 22]
          }
        ]
      },
      {
        "SubType": "c",
        "SubTypeNr": 3,
        "SubTypeData": [
          {
            "SubTypeVal1": [25, 29, 33],
            "SubTypeVal2": [26, 30, 34]
          }
        ]
      },
      {
        "SubType": "d",
        "SubTypeNr": 4,
        "SubTypeData": [
          {
            "SubTypeVal1": [37, 41],
            "SubTypeVal2": [38, 42]
          }
        ]
      }
    ]
  },
  {
    "Type": "B",
    "TypeData": [
      {
        "SubType": "e",
        "SubTypeNr": 5,
        "SubTypeData": [
          {
            "SubTypeVal1": [2, 7],
            "SubTypeVal2": [3, 8]
          }
        ]
      },
      {
        "SubType": "f",
        "SubTypeNr": 6,
        "SubTypeData": [
          {
            "SubTypeVal1": [12, 17, 22],
            "SubTypeVal2": [13, 18, 23]
          }
        ]
      }
    ]
  }
]