i'm working httr package in r in attempt query postcode.io api (http://postcodes.io/docs).
i can query single postcode per instructions using: sample4 <- get("api.postcodes.io/postcodes/en14rf")
when try , query multiple postcodes i'm becoming little unstuck. postcode.io instructions suggest
post https://api.postcodes.io/postcodes?q=[postcode]
where json object containing array of postcodes specified. have r vector containing postcodes have attempted convert json object such with:
a <- tojson(a)
where r vector 'a' is:
structure(c(4l, 5l, 3l, 6l, 1l, 2l), .label = c("bn14 9aw", "cr0 4be", "e5 8hb", "en1 4rf", "g42 8qn", "sa1 3ul"), class = "factor")
now when try , query api following line of code:
sample4 <- post("https://api.postcodes.io/postcodes?q=[postcode]", body = list(postcode = add1json))
i error: "invalid json submitted. need submit json object array of postcodes or geolocation objects"
i have feeling because not providing array unnamed list e.g. json object should this:
{"postcodes":"[ \"en14rf\", \"g428qn\", \"e58hb\", \"sa13ul\", \"bn149aw\", \"cr04be\" ]"}
not this: "[ \"en14rf\", \"g428qn\", \"e58hb\", \"sa13ul\", \"bn149aw\", \"cr04be\" ]"
can me this? i've feeling tojson
call, have been unable find similar example on forum or on api developers page:(
many thanks
marty
they have poorly written api docs. seems intent of particular api call:
library(httr) library(jsonlite) library(dplyr) post_codes <- c("bn14 9aw", "cr0 4be", "e5 8hb", "en1 4rf", "g42 8qn", "sa1 3ul") res <- post("https://api.postcodes.io/postcodes", body=list(postcodes=post_codes), encode="json") status_code(res) ## [1] 200 content(res, as="text") %>% fromjson(flatten=true) %>% glimpse() ## list of 2 ## $ status: int 200 ## $ result:'data.frame': 6 obs. of 29 variables: ## ..$ query : chr [1:6] "bn14 9aw" "e5 8hb" "cr0 4be" "en1 4rf" ... ## ..$ result.postcode : chr [1:6] "bn14 9aw" "e5 8hb" "cr0 4be" "en1 4rf" ... ## ..$ result.quality : int [1:6] 1 1 1 1 1 1 ## ..$ result.eastings : int [1:6] 514948 534934 531978 534957 264583 258092 ## ..$ result.northings : int [1:6] 104386 185332 164963 199610 192273 662417 ## ..$ result.country : chr [1:6] "england" "england" "england" "england" ... ## ..$ result.nhs_ha : chr [1:6] "south east coast" "london" "london" "london" ... ## ..$ result.longitude : num [1:6] -0.3693 -0.0553 -0.1055 -0.0494 -3.9572 ... ## ..$ result.latitude : num [1:6] 50.8 51.6 51.4 51.7 51.6 ... ## ..$ result.parliamentary_constituency: chr [1:6] "east worthing , shoreham" "hackney south , shoreditch" "croydon south" "enfield north" ... ## ..$ result.european_electoral_region : chr [1:6] "south east" "london" "london" "london" ... ## ..$ result.primary_care_trust : chr [1:6] "west sussex" "city , hackney teaching" "croydon" "enfield" ... ## ..$ result.region : chr [1:6] "south east" "london" "london" "london" ... ## ..$ result.lsoa : chr [1:6] "worthing 008c" "hackney 017b" "croydon 024d" "enfield 002e" ... ## ..$ result.msoa : chr [1:6] "worthing 008" "hackney 017" "croydon 024" "enfield 002" ... ## ..$ result.incode : chr [1:6] "9aw" "8hb" "4be" "4rf" ... ## ..$ result.outcode : chr [1:6] "bn14" "e5" "cr0" "en1" ... ## ..$ result.admin_district : chr [1:6] "worthing" "hackney" "croydon" "enfield" ... ## ..$ result.parish : chr [1:6] "worthing, unparished area" "hackney, unparished area" "croydon, unparished area" "enfield, unparished area" ... ## ..$ result.admin_county : chr [1:6] "west sussex" na na na ... ## ..$ result.admin_ward : chr [1:6] "gaisford" "homerton" "waddon" "turkey street" ... ## ..$ result.ccg : chr [1:6] na "nhs city , hackney" "nhs croydon" "nhs enfield" ... ## ..$ result.nuts : chr [1:6] "west sussex (south west)" "hackney , newham" "croydon" "enfield" ... ## ..$ result.codes.admin_district : chr [1:6] "e07000229" "e09000012" "e09000008" "e09000010" ... ## ..$ result.codes.admin_county : chr [1:6] "e10000032" "e99999999" "e99999999" "e99999999" ... ## ..$ result.codes.admin_ward : chr [1:6] "e05007698" "e05009376" "e05000167" "e05000211" ... ## ..$ result.codes.parish : chr [1:6] "e43000150" "e43000202" "e43000198" "e43000200" ... ## ..$ result.codes.ccg : chr [1:6] "e38000213" "e38000035" "e38000040" "e38000057" ... ## ..$ result.codes.nuts : chr [1:6] "ukj27" "uki41" "uki62" "uki54" ...
Comments
Post a Comment