json - What does 'records are values not objects' mean in tidyjson -
the json in following string correct json according http://jsonlint.com/ tidyjson objects:
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. work in 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 i get:
error in gather_keys(.) : 1 records values not objects
as mentioned in 1 of comments, gather_keys looking objects, have array. should using here gather_array.
further, other answer uses more brute-force approach parsing json attribute tidyjson package creates. tidyjson provides methods dealing in bit cleaner pipeline if desired:
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. work in 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')) this pipeline shines if there multiple such objects in array. hope helpful, if long after fact!
Comments
Post a Comment