什么'记录是值而不是对象'在tidyjson

时间:2016-01-06 01:22:58

标签: json r

以下字符串中的json根据http://jsonlint.com/是正确的json但是tidyjson对象:

library(dplyr)
library(tidyjson)

json <- '
    [{"country":"us","city":"Portland","topics":[{"urlkey":"videogame","name":"Video Games","id":4471},{"urlkey":"board-games","name":"Board Games","id":19585},{"urlkey":"computer-programming","name":"Computer programming","id":48471},{"urlkey":"opensource","name":"Open Source","id":563}],"joined":1416349237000,"link":"http://www.meetup.com/members/156440062","bio":"Analytics engineer.  Primarily work in the Hadoop space.","lon":-122.65,"other_services":{},"name":"Aaron Wirick","visited":1443078098000,"self":{"common":{}},"id":156440062,"state":"OR","lat":45.56,"status":"active"}]
    '
    json %>% as.tbl_json %>% gather_keys

我明白了:

Error in gather_keys(.) : 1 records are values not objects

2 个答案:

答案 0 :(得分:1)

正如其中一条评论所述,gather_keys正在寻找具有数组的对象。你应该在这里使用的是gather_array

此外,另一个答案使用更强力的方法来解析tidyjson包创建的JSON属性。如果需要,tidyjson提供了在更清洁的管道中处理这个问题的方法:

library(dplyr)
library(tidyjson)

json <- '
[{"country":"us","city":"Portland"
,"topics":[
 {"urlkey":"videogame","name":"Video Games","id":4471}
 ,{"urlkey":"board-games","name":"Board Games","id":19585}
 ,{"urlkey":"computer-programming","name":"Computer programming","id":48471}
 ,{"urlkey":"opensource","name":"Open Source","id":563}
]
,"joined":1416349237000
,"link":"http://www.meetup.com/members/156440062"
,"bio":"Analytics engineer.  Primarily work in the Hadoop space."
,"lon":-122.65,"other_services":{}
,"name":"Aaron Wirick","visited":1443078098000
,"self":{"common":{}}
,"id":156440062,"state":"OR","lat":45.56,"status":"active"
}]
'

mydf <- json %>% as.tbl_json %>% gather_array %>% 
spread_values(
 country=jstring('country')
 , city=jstring('city')
 , joined=jnumber('joined')
 , bio=jstring('bio')
) %>% 
enter_object('topics') %>% 
gather_array %>%
spread_values(urlkey=jstring('urlkey'))

如果阵列中有多个这样的对象,这个管道真的很棒。希望有用,即使事后很长时间!

答案 1 :(得分:0)

as.tbl_json生成的对象对我的思维方式有点奇怪,其中一个项目的名称为document.id,值为1.其属性中有一个名为{{ 1}}:

JSON

看看你可以看到,为了获得嵌入式列表对象的名称,你需要这样做:

json <- '
     [{"country":"us","city":"Portland","topics":[{"urlkey":"videogame","name":"Video Games","id":4471},{"urlkey":"board-games","name":"Board Games","id":19585},{"urlkey":"computer-programming","name":"Computer programming","id":48471},{"urlkey":"opensource","name":"Open Source","id":563}],"joined":1416349237000,"link":"http://www.meetup.com/members/156440062","bio":"Analytics engineer.  Primarily work in the Hadoop space.","lon":-122.65,"other_services":{},"name":"Aaron Wirick","visited":1443078098000,"self":{"common":{}},"id":156440062,"state":"OR","lat":45.56,"status":"active"}]
     '
    obj <-  json %>% as.tbl_json

> dput(obj)
structure(list(document.id = 1L), .Names = "document.id", row.names = 1L, class = c("tbl_json", 
"tbl", "data.frame"), JSON = list(list(structure(list(country = "us", 
    city = "Portland", topics = list(structure(list(urlkey = "videogame", 
        name = "Video Games", id = 4471L), .Names = c("urlkey", 
    "name", "id")), structure(list(urlkey = "board-games", name = "Board Games", 
        id = 19585L), .Names = c("urlkey", "name", "id")), structure(list(
        urlkey = "computer-programming", name = "Computer programming", 
        id = 48471L), .Names = c("urlkey", "name", "id")), structure(list(
        urlkey = "opensource", name = "Open Source", id = 563L), .Names = c("urlkey", 
    "name", "id"))), joined = 1416349237000, link = "http://www.meetup.com/members/156440062", 
    bio = "Analytics engineer.  Primarily work in the Hadoop space.", 
    lon = -122.65, other_services = structure(list(), .Names = character(0)), 
    name = "Aaron Wirick", visited = 1443078098000, self = structure(list(
        common = structure(list(), .Names = character(0))), .Names = "common"), 
    id = 156440062L, state = "OR", lat = 45.56, status = "active"), .Names = c("country", 
"city", "topics", "joined", "link", "bio", "lon", "other_services", 
"name", "visited", "self", "id", "state", "lat", "status")))))

希望我能得到更多帮助,但至少你了解错误的来源。 (我也希望在该软件包的帮助页面上有更多示例。)