将 Json 解析为 Dataframe R 时出错

Error in Parsing Json to Dataframe R

我在传递 Json 数据帧时遇到下面提到的错误。

Error in data.frame(Addr = "London", companyName = "Eagle SUITS",  : 
  arguments imply differing number of rows: 1, 0, 2 In addition: Warning message:
In data.frame(Addr = "NA", companyName = "SAMTEK ENTERPRISES",  :
  row names were found from a short variable and have been discarded

我的数据框:

view(df_1)

json_data                id
{Data in json format}    123
{Data in json format}    456
{Data in json format}    789

我正在使用下面提到的代码:

library(jsonlite)
json_dfs <- mapply(f, df_1$json_data, df_1$id, SIMPLIFY = FALSE)

来自OP的JSON文件内容: 查看(df_1$json_data)

问题似乎出在函数 f 中,该函数用于转换 json 数据并将 ID 与 JSON 合并。 JSON 数据是嵌套数据,因此最好将 append 作为解析 json 后创建的 list 的一部分,然后将其转换为所需格式。

我建议将函数修改为:

f_JSON_Transform <- function(json, id){ 
  # transform json to list 
  tmp <- jsonlite::fromJSON(json) 
  # Append ID in list 
  tmp <- c(tmp, ID = id)
  # return 
  return(tmp) 
}
# Below call works without any error
json_lists <- mapply(f_JSON_Transform, df1$json_data, df1$id, SIMPLIFY = FALSE)

#Result
#Output is too long to write here...but it is like
#$test.txt$agreementOfficeAddr
#[1] "NA"

#$test.txt$companyName
#[1] "SAMTEK ENTERPRISES"

#$test.txt$companyAddress
#[1] "E-524,GALI NO- 45, NEAR CHAND MASJID,SHASTRI PARK"
#......
#......