首页 > 解决方案 > 将 JSON 从文件读取到 tidyjson

问题描述

我正在尝试从下面的位置读取 json 并使用 tidyjson 创建一个整洁的版本。

https://raw.githubusercontent.com/mysociety/parlparse/master/members/people.json

tidyjson 的指南没有提到如何读取文件。我尝试了各种方法,包括 fromJSON、read_JSON、readJSON 和 read_lines,但在每个示例中都遇到了错误。

例如

peopleJSON <- read_lines(url("https://raw.githubusercontent.com/mysociety/parlparse/master/members/people.json"))
people <- peopleJSON  %>% as.tbl_json

Error: parse error: premature EOF
                                       {
                     (right here) ------^

我不确定我是否做错了什么或者问题是否与数据有关。

任何帮助都将受到热烈欢迎。

标签: rjsontidyverse

解决方案


fromJSONfromjsonlite返回 4 个数据帧的列表。

data <- jsonlite::fromJSON('https://raw.githubusercontent.com/mysociety/parlparse/master/members/people.json')

sapply(data, dim)

#     memberships organizations persons posts
#[1,]       45674            43   13902  2370
#[2,]          15             4       5     8

data$memberships您可以使用等访问单个数据框data$organizations


推荐阅读