在 R 中跳过 JSON 中的 NULL 值

Skip NULL value in JSON in R

大家好! 我有用于解析 JSON 文件的简单 R 脚本:

json <- 
   rjson::fromJSON(readLines('http://data.rada.gov.ua/ogd/zpr/skl8/bills-
   skl8.json', warn=F))
bills <-  data.frame(
  id = numeric(), 
  title = character(),
  type = character(), 
  subject = character(), 
  rubric = character(),
  executive = character(),
  sesion = character(),
  result = character() 
)
for (row in json) 
{
  bill <- data.frame(
    id = row$id, 
    title = row$title, 
    type = row$type,
    subject = row$subject, 
    rubric = row$rubric,
    executive = row$mainExecutives$executive$department,
    sesion = row$registrationSession,
    result = row$currentPhase$title
)
  bills <- rbind(bills, bill)
}

但我在 data.frame(id = row$id, title = row$title, type = row$type, subject = row$subject, : 参数表示不同的行数:1、0

所以,我的 JSON 文件在 277 行中有 NULL 值。我可以跳过这个错误或在我的循环中替换 NULL 值吗? 谢谢!

为了回答您的直接问题,我会用一个小函数包装它,如果执行部门缺失,returns 一个字符串。

protect_against_null <- function( x ) {
  if( is.null(x) )
    return( "" ) # Replace with whatever string you'd like.
  else 
    return( x )
}

for (row in json) {
  bill <- data.frame(
    id = row$id, 
    title = row$title, 
    type = row$type,
    subject = row$subject, 
    rubric = row$rubric,
    executive = protect_against_null(row$mainExecutives$executive$department),
    sesion = row$registrationSession,
    result = row$currentPhase$title
  )
  bills <- rbind(bills, bill)
}

Long-term 建议:由于此数据集包含 11,000 条嵌套记录,因此我会避免使用循环。查看 purrr package for mapping the nested json/list into a rectangular data frame. Especially purrr::map_dfr().

为此目的 fromJSONjsonlite 包)可能很方便。

library(jsonlite)

url <- 'http://data.rada.gov.ua/ogd/zpr/skl8/bills-skl8.json'
df <- jsonlite::fromJSON(url)   

df1 <- data.frame(
  id = df$id, 
  title = df$title, 
  type = df$type,
  subject = df$subject, 
  rubric = df$rubric,
  executive = df$mainExecutives$executive$department,
  sesion = df$registrationSession,
  result = df$currentPhase$title
)