2017-07-19 63 views
2

我想使用R tidyjson包的开发者版本从JSON对象中拉选择数组。我想从下面的示例JSON对象创建下面的示例表。任何帮助,将不胜感激。使用tidyjson从JSON中拉数组

下面是表我试图创建:

document.id location.lat location.lng viewport  name place_id 
1    32.123451  -85.234541  northeast Name1 sdfdfasdfasdfdasfdas 
1    32.123451  -85.234541  southwest Name1 sdfdfasdfasdfdasfdas 
2    33.345454  -84.345454  northeast Name2 sdfdsfdsfdff 
2    33.345454  -84.345454  southwest Name2 sdfdsfdsfdff 

这里是我的JSON对象:

JSON_TEST <- "{ 
    \"html_attributions\" : [], 
\"results\" : [ 
{ 
    \"geometry\" : { 
    \"location\" : { 
    \"lat\" : 32.123451, 
    \"lng\" : -85.234541 
}, 
    \"viewport\" : { 
    \"northeast\" : { 
    \"lat\" : 32.234341, 
    \"lng\" : -85.345655 
}, 
    \"southwest\" : { 
    \"lat\" : 32.235624, 
    \"lng\" : -85.234655 
} 
} 
}, 
\"icon\" : \"https://fake/fake/fake1.png\", 
\"id\" : \"qwerqewrqwerqewrqewrqwreqewrqewrqwr\", 
\"name\" : \"Name1\", 
\"place_id\" : \"sdfdfasdfasdfdasfdas\", 
\"reference\" : \"asdfdasfadsfdasfdfdfdffff\", 
\"scope\" : \"TEST1\", 
\"types\" : [ 
\"bar\", 
\"liquor_store\", 
\"food\", 
\"store\", 
\"point_of_interest\", 
\"establishment\" 
], 
\"vicinity\" : \"343 Fake Place Lane, Atlanta\" 
}, 
{ 
    \"geometry\" : { 
    \"location\" : { 
    \"lat\" : 33.345454, 
    \"lng\" : -84.345454 
}, 
    \"viewport\" : { 
    \"northeast\" : { 
    \"lat\" : 33.234534 
    \"lng\" : -84.234643 
}, 
    \"southwest\" : { 
    \"lat\" : 33.345443, 
    \"lng\" : -84.345422 
} 
} 
}, 
\"icon\" : \"https://fake/fake/fake2.png\", 
\"id\" : \"sdfdsfdsfdff\", 
\"name\" : \"Name2\", 
\"place_id\" : \"sadfsdfdfdf\", 
\"reference\" : \"asdfdasfdsfd\", 
\"scope\" : \"TEST2\", 
\"types\" : [ \"bar\", \"point_of_interest\", \"establishment\" ], 
\"vicinity\" : \"21434 Fake Place Ave, Atlanta\" 
} 
], 
\"status\" : \"OK\" 
} 
" 
+0

你错过一对夫妇逃逸和一个逗号。 – alistaire

+0

感谢alistaire,我只是编辑了JSON对象并相信我已经修复了转义和逗号。 – Bdude11383

+0

根据您的[其他问题](https://stackoverflow.com/q/45242400/5977215),您可以使用我的'googleway'包直接查询Google Places API – SymbolixAU

回答

2

希望它能帮助!

JSON_TEST <- 
"{\"html_attributions\" : [], 
    \"results\" : [ 
    {\"geometry\" : {\"location\" : {\"lat\" : 32.123451,\"lng\" : -85.234541}, 
        \"viewport\" : {\"northeast\" : {\"lat\" : 32.234341,\"lng\" : -85.345655}, 
            \"southwest\" : {\"lat\" : 32.235624,\"lng\" : -85.234655} 
            } 
        }, 
    \"icon\" : \"https://fake/fake/fake1.png\", 
    \"id\" : \"qwerqewrqwerqewrqewrqwreqewrqewrqwr\", 
    \"name\" : \"Name1\", 
    \"place_id\" : \"sdfdfasdfasdfdasfdas\", 
    \"reference\" : \"asdfdasfadsfdasfdfdfdffff\", 
    \"scope\" : \"TEST1\", 
    \"types\" : [\"bar\",\"liquor_store\",\"food\",\"store\",\"point_of_interest\",\"establishment\"], 
    \"vicinity\" : \"343 Fake Place Lane, Atlanta\" 
    }, 
    {\"geometry\" : {\"location\" : {\"lat\" : 33.345454,\"lng\" : -84.345454}, 
        \"viewport\" : {\"northeast\" : {\"lat\" : 33.234534,\"lng\" : -84.234643}, 
            \"southwest\" : {\"lat\" : 33.345443,\"lng\" : -84.345422} 
            } 
        }, 
    \"icon\" : \"https://fake/fake/fake2.png\", 
    \"id\" : \"sdfdsfdsfdff\", 
    \"name\" : \"Name2\", 
    \"place_id\" : \"sadfsdfdfdf\", 
    \"reference\" : \"asdfdasfdsfd\", 
    \"scope\" : \"TEST2\", 
    \"types\" : [ \"bar\", \"point_of_interest\", \"establishment\" ], 
    \"vicinity\" : \"21434 Fake Place Ave, Atlanta\" 
    } 
    ], 
\"status\" : \"OK\" 
}" 

#devtools::install_github("sailthru/tidyjson") 
library(tidyjson) 
library(dplyr) 
JSON_TEST <- gsub("\\n","",JSON_TEST) 
JSON_TEST %>%   
    as.tbl_json %>% 
    enter_object("results") %>% 
    gather_array %>% 
    spread_values( 
     name = jstring("name"), 
     place_id = jstring("place_id") 
) %>% 
    enter_object("geometry") %>% 
    spread_values( 
    location.lat = jnumber("location","lat"), 
    location.lng = jnumber("location","lng") 
    ) %>% 
    enter_object("viewport") %>% 
    gather_keys("viewport") 

不要忘了让我们知道是否能解决你的问题:)

+0

!您是否以某种方式修改了我的原始JSON_TEST对象?您的代码完美地运行在您的答案中嵌入的JSON_TEST对象上,但是当我在原始JSON_TEST上运行时出现以下错误: “Error:parse error:after key and value,inside map,I expect','or'} ' st“:{”lat“:33.234534”lng“:-84.234643},”西南 (就在这里)------ ^“ – Bdude11383

+0

是的,一个逗号在'\”viewport \“之后丢失:{ \“东北部\”:{\“lat \”:33.234534'。顺便说一句 - 如果它解决了你的问题,那么你应该接受(或投票)它作为正确的答案:) – Prem

+0

明白了吧!作品非常感谢! – Bdude11383

1

您的数据看起来像是来自谷歌的Places API的。因此,您可以绕过JSON格式,用我googleway包得到的结果为你

library(googleway) 

api_key <- 'your_api_key' 

myPlaces <- google_places(search_string = "Restaurants in Melbourne", 
         key = api_key) 

the results come back as a list in R, so you can grab the pieces of information directly 

head(cbind(myPlaces$results$geometry, myPlaces$results$place_id)) 

# location.lat location.lng viewport.northeast.lat viewport.northeast.lng viewport.southwest.lat viewport.southwest.lng 
# 1  28.07650 -80.59910    28.07842    -80.59773    28.07572    -80.60043 
# 2  28.07724 -80.60456    28.07843    -80.60320    28.07573    -80.60590 
# 3  28.07872 -80.60723    28.07993    -80.60588    28.07723    -80.60858 
# 4  28.07950 -80.60212    28.08073    -80.60069    28.07803    -80.60339 
# 5  28.21043 -80.66411    28.21174    -80.66287    28.20904    -80.66557 
# 6  28.07839 -80.60321    28.07982    -80.60167    28.07712    -80.60437 
# myPlaces$results$place_id 
# 1 ChIJSzCXdo8R3ogRodiPcpYYLGw 
# 2 ChIJl_1IpI4R3ogR50nk7fYHdb8 
# 3 ChIJ2QdwWowR3ogRKJOrSqPQuYU 
# 4 ChIJK_gOU44R3ogRGXv8ScI7-t0 
# 5 ChIJxytL4RwF3ogRw9qGq8mSm5w 
# 6 ChIJa7H_5I4R3ogRys0um892_VA