首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并Json文件中的副本

合并Json文件中的副本
EN

Stack Overflow用户
提问于 2022-02-10 11:58:22
回答 1查看 44关注 0票数 1

我的JSON文件如下所示

代码语言:javascript
复制
 [
  {
    "Hugo_Symbol": "FLT3",
    "HGVSp_Short": "p.T227M",
    "Other": {
      "Type": "Renal",
      "Drug": "Sunitinib",
      "Effects": "Response"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Ovarian",
      "Drug": "Cetuximab",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Lung",
      "Drug": "Regorafenib",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Leukemia",
      "Drug": "Akt",
      "Effects": "Resistance"
    }
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": {
      "Type": "Lung",
      "Drug": "gefitimab",
      "Effects": "Sensitivity"
    }
  },
  {
    "Hugo_Symbol": "ALK",
    "HGVSp_Short": "p.G845=",
    "Other": {
      "Type": "",
      "Drug": "",
      "Effects": ""
    }
  },
  {
    "Hugo_Symbol": "APC",
    "HGVSp_Short": "p.Q1338*",
    "Other": {
      "Type": "",
      "Drug": "",
      "Effects": ""
    }
  },
  {
    "Hugo_Symbol": "MET",
    "HGVSp_Short": "p. M1S",
    "Other": {
      "Type": "Eye",
      "Drug": "",
      "Effects": "Response"
    }
  }
]

我想合并我的JSON文件中的副本

预期产出

代码语言:javascript
复制
[
    {
      "Hugo_Symbol": "FLT3",
      "HGVSp_Short": "p.T227M",
      "Other": [ 
      {"Type": "Renal", "Drug": "Sunitinib","Effect": "Response" }
      ]
    },
    {
      "Hugo_Symbol": "KRAS",
      "HGVSp_Short": "p.G12D",
      "Other": [ 
      {"Type": "Ovarian","Drug": "Cetuximab","Effect": "Sensitivity"},
      {"Type": "Lung","Drug": "Regorafenib","Effect": "Sensitivity"},
      {"Type": "Leukemia","Drug": "Akt","Effect": "Resistance"},
      {"Type": "Lung", "Drug": "gefitimab", "Effect": "Sensitivity"}
      ]
    },
    {
      "Hugo_Symbol": "ALK",
      "HGVSp_Short": "p.G845=",
      "Other": [ 
      {"Type": "", "Drug": "","Effect": "" }
      ]
    },
    {
      "Hugo_Symbol": "APC",
      "HGVSp_Short": "p.Q1338*",
      "Other":[
       {"Type": "","Drug": "","Effect": ""}
       ]
     },
     {
      "Hugo_Symbol": "MET",
      "HGVSp_Short": "p.M1S",
      "OtherTumorInfo": [
      {"Type": "Eye","Drug": "","Effect": "Response" }
      ]
     }
  ]

我的初始文件是一个CSV文件,我使用R将我的CSV文件转换成JSON

代码语言:javascript
复制
func <- function(x) {
     grps <- split(names(x), gsub("[.].*", "", names(x)))
     for (nm in names(grps)) {
         if (length(grps[[nm]]) > 1 || !nm %in% grps[[nm]]) {
             x[[nm]] <- setNames(subset(x, select = grps[[nm]]),
                                 gsub("^[^.]+[.]", "", grps[[nm]]))
             x[,setdiff(grps[[nm]], nm)] <- NULL
         }
     }
     for (nm in names(x)) {
         if (is.data.frame(x[[nm]])) {
             x[[nm]] <- func(x[[nm]])
         }
     }
     if (any(grepl("[.]", names(x)))) func(x) else x
 }

我使用这个函数将我的CSV文件转换为嵌套的JSON文件

代码语言:javascript
复制
newdf= func(x)

将数据帧转换为JSON

代码语言:javascript
复制
library(jsonlite)
json_f=toJSON(newdf,dataframe = 'rows',pretty = T)

我的CSV文件如下

代码语言:javascript
复制
| Hugo_Symbol | HGVSp_Short | Other.Type | Other.Drug | Other.Effects |
| ----------- | ----------- | ---------- | ---------- | ------------- | 
|FLT3         | p.T227M     | Renal      | Sunitinib  | Response      |
|KRAS         | p.G12D      | Ovarian    | Cetuximab  | Sensitivity   |
|KRAS         | p.G12D      | Lung       | Regorafenib| Sensitivity   |
|KRAS         | p.G12D      | Leukemia   | Akt        | Resistance    |
|KRAS         | p.G12D      | Lung       | gefitimab  | Sensitivity   |
|ALK          | p.G845=     |            |            |               |
|APC          | p.Q1338*    |            |            |               |
|MET          | p. M1S      | Eye        |            | Response      |

有人能建议我一种简单的方法来合并R中JSON文件中的重复数组吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-10 12:35:39

在表上进行重新嵌套要容易得多,因此让我们将json转换回tibble:

代码语言:javascript
复制
library(jsonlite)
library(tidyverse)

read_json("data.json") %>%
  map(as.data.frame) %>%
  bind_rows() %>%
  as_tibble() %>%
  nest(-Hugo_Symbol, -HGVSp_Short) %>%
  mutate(data = data %>% map(~ {
    colnames(.x) <- colnames(.x) %>% str_remove("^Other.")
    .x
  })) %>%
  rename(Other = data) %>%
  toJSON(pretty = T)

结果是

代码语言:javascript
复制
[
  {
    "Hugo_Symbol": "FLT3",
    "HGVSp_Short": "p.T227M",
    "Other": [
      {
        "Type": "Renal",
        "Drug": "Sunitinib",
        "Effects": "Response"
      }
    ]
  },
  {
    "Hugo_Symbol": "KRAS",
    "HGVSp_Short": "p.G12D",
    "Other": [
      {
        "Type": "Ovarian",
        "Drug": "Cetuximab",
        "Effects": "Sensitivity"
      },
      {
        "Type": "Lung",
        "Drug": "Regorafenib",
        "Effects": "Sensitivity"
      },
      {
        "Type": "Leukemia",
        "Drug": "Akt",
        "Effects": "Resistance"
      },
      {
        "Type": "Lung",
        "Drug": "gefitimab",
        "Effects": "Sensitivity"
      }
    ]
  },
  {
    "Hugo_Symbol": "ALK",
    "HGVSp_Short": "p.G845="
...
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71064718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档