将嵌套的json转换为数据框时出错

问题描述

我有一列带有嵌套json的dtaframe。我想把其中之一变成数据框。那个json在这里:df [1,] $ json,看起来像这样:

{"case": "payment: fraud checking","params": {},"answer": {"assistant_check": false}}

我尝试这样做:

library(jsonlite)

df[1,]$json %>% 
  fromJSON() %>%
  as.data.frame()

但这给了我这个错误:

Error in (function (...,row.names = NULL,check.rows = FALSE,check.names = TRUE,: 
  arguments imply differing number of rows: 1,0

为什么会这样?我猜是因为“参数”:{}。怎么解决呢?为什么我没有从Dong那里得到这个数据框:

       case                 params    answer.assistant_check
payment: fraud checking      NA            FALSE

解决方法

如果您的json列中不包含任何数组数据,这将对您有用(但是如果所有行上的行都为空,则消除params

library(dplyr)
library(purrr)
library(jsonlite)

df$json %>%
    map_dfr(~unlist(jsonlite::fromJSON(.)))

这对您有帮助吗?

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...