使用tidyjson

时间:2017-07-19 16:07:24

标签: json r

我正在尝试使用R tidyjson软件包的开发者版本从JSON对象中拉取选择数组。我想从下面的示例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\"
}
"

2 个答案:

答案 0 :(得分: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")

如果它解决了您的问题,请不要忘记告诉我们:)

答案 1 :(得分:1)

您的数据看起来来自Google的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