Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不知道密钥的情况下从R数据帧中提取嵌套JSON_Json_R_Dplyr_Purrr - Fatal编程技术网

在不知道密钥的情况下从R数据帧中提取嵌套JSON

在不知道密钥的情况下从R数据帧中提取嵌套JSON,json,r,dplyr,purrr,Json,R,Dplyr,Purrr,我试图从TSV列中提取JSON。困难在于JSON嵌套很浅,并且键值可能不存在于每一行中 我有一个简单的例子来说明我的观点 df <- tibble(index = c(1, 2), data = c('{"json_char":"alpha", "json_list1":["x","y"]}', '{"json_char":"beta", "json_list1":["x","y","z"], "json_list2":["a","b","c"]}')) d

我试图从TSV列中提取JSON。困难在于JSON嵌套很浅,并且键值可能不存在于每一行中

我有一个简单的例子来说明我的观点

  df <- tibble(index = c(1, 2),
   data = c('{"json_char":"alpha", "json_list1":["x","y"]}', 
         '{"json_char":"beta", "json_list1":["x","y","z"], "json_list2":["a","b","c"]}'))
df%
purrr::map(purrr::simplify)%%>%
tibble::enframe()%>%
tidyr::价差(“名称”、“价值”)%>%
purrr::展平
}
这给了我以下错误:
绑定行中的错误(x,.id):参数2的长度必须是3,而不是7


第一行设置数据帧其余部分的参数数量。有没有办法避免这种行为

我将您的函数修改为以下内容。我希望这有帮助

library(tidyverse)
library(rjson)

extract_json_column <- function(df){
  df %>%
    rowwise() %>%
    mutate(data = map(data, fromJSON)) %>%
    split(.$index) %>%
    map(~.$data[[1]]) %>%
    map(~map_if(., function(x) length(x) != 1, list)) %>%
    map(as_data_frame) %>%
    bind_rows(.id = "index")
}

extract_json_column(df)
# A tibble: 2 x 4
  index json_char json_list1 json_list2
  <chr>     <chr>     <list>     <list>
1     1     alpha  <chr [2]>     <NULL>
2     2      beta  <chr [3]>  <chr [3]>
库(tidyverse)
图书馆(rjson)
提取列%
行()
mutate(data=map(data,fromJSON))%>%
分割(.$指数)%>%
地图(~.$data[[1]])%>%
映射(~map_if(,函数(x)长度(x)!=1,列表))%>%
映射(作为数据帧)%>%
绑定行(.id=“index”)
}
提取json列(df)
#一个tibble:2x4
索引json\u char json\u列表1 json\u列表2
1阿尔法
2测试版
extract_json_column <- function(df) {
  df %>%
    magrittr::use_series(data) %>% 
    purrr::map(jsonlite::fromJSON) %>% 
    purrr::map(purrr::simplify) %>% 
    tibble::enframe() %>% 
    tidyr::spread("name", "value") %>%
    purrr::flatten_dfr()
}
library(tidyverse)
library(rjson)

extract_json_column <- function(df){
  df %>%
    rowwise() %>%
    mutate(data = map(data, fromJSON)) %>%
    split(.$index) %>%
    map(~.$data[[1]]) %>%
    map(~map_if(., function(x) length(x) != 1, list)) %>%
    map(as_data_frame) %>%
    bind_rows(.id = "index")
}

extract_json_column(df)
# A tibble: 2 x 4
  index json_char json_list1 json_list2
  <chr>     <chr>     <list>     <list>
1     1     alpha  <chr [2]>     <NULL>
2     2      beta  <chr [3]>  <chr [3]>