2017-07-19 11 views
2

私は開発者用のR tidyjsonパッケージを使用してJSONオブジェクトから選択配列をプルしようとしています。以下のJSONオブジェクトの例から、下のサンプル・テーブルを作成したいと思います。これに関する助けがあれば大いに感謝します。ここでJSONからtidyjsonを使用して配列を引き出す

は、私が作成しようとしている表である。ここでは

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

ありがとうございました。私は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を実行すると、次のエラーが発生します。 "エラー:解析エラー:キーと値の後、マップ内に '、'または ' " st":{"lat":33.234534 "lng":-84.234643} "南西 (右)------ ^" – Bdude11383

+0

はい、ビューポートの後に1つのカンマがありません: "{ \ "北東\":{\ "lat \":33.234534}。ところで - それがあなたの問題を解決したら、あなたは正しい答えとしてそれを受け入れるべきです(または投票する)。 – Prem

+0

これを今すぐ入手してください!美しい作品をありがとう! – Bdude11383

1

それはGoogleの場所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 
関連する問題