Reputation: 90
I'm an API newbie teaching myself how to interface with Planet Labs' API in R, but Planet's documentation is for Python, which I'm not familiar with (hence working in R).
On one of their tutorials, they provide some python/curl/jq code that returns a column from a dataframe in a list.
➜ curl -L -H "Authorization: api-key $PL_API_KEY" \
'https://api.planet.com/data/v1/item-types' | jq '.item_types[].id'
"REOrthoTile" # This is the desired output
"PSOrthoTile" # Desired output
My question is this: how do I incorporate the jq portion of the code above into my API request in R such that the response only contains the relevant column rather than the whole list and dataframe? I can get there the long way around by subsetting from the larger list and dataframe, but I'm trying to avoid muddying up the API response with all that unneeded information.
library(httr)
library(jsonlite)
key = "my_Planet_API_key"
res = GET("https://api.planet.com/data/v1/item-types",
authenticate(user = key, password = ""))
>res
Response [https://api.planet.com/data/v1/item-types]
Date: 2020-09-11 17:29
Status: 200
Content-Type: application/json
Size: 7.43 kB
{"_links":{"_self":"https://api.planet.com/data/v1/item-type...
data = fromJSON(rawToChar(res$content))
>summary(data)
Length Class Mode
_links 1 -none- list
item_types 5 data.frame list
> str(data, vec.len = 1)
List of 2
$ _links :List of 1
..$ _self: chr "https://api.planet.com/data/v1/item-types/"
$ item_types:'data.frame': 15 obs. of 5 variables:
..$ _links :'data.frame': 15 obs. of 1 variable:
.. ..$ _self: chr [1:15] "https://api.planet.com/data/v1/item-types/PSOrthoTile" ...
..$ display_description : chr [1:15] "PlanetScope orthorectified 4-band imagery as 25km x 25km UTM tiles" ...
..$ display_name : chr [1:15] "PlanetScope Ortho Tile" ...
..$ id : chr [1:15] "PSOrthoTile" ...
..$ supported_asset_types:List of 15
.. ..$ : chr [1:11] "analytic" ...
.. ..$ : chr [1:6] "analytic" ...
.. ..$ : chr [1:16] "analytic" ...
.. ..$ : chr [1:23] "analytic" ...
.. ..$ : chr [1:16] "basic_analytic_b1" ...
.. ..$ : chr [1:14] "analytic_b1" ...
.. ..$ : chr [1:15] "analytic_b1" ...
.. ..$ : chr [1:26] "analytic" ...
.. ..$ : chr [1:12] "ortho_analytic" ...
.. ..$ : chr [1:3] "video_file" ...
.. ..$ : chr [1:4] "ortho_analytic_hh" ...
.. ..$ : chr [1:22] "analytic_gflags" ...
.. ..$ : chr [1:8] "analytic_granule_pnt" ...
.. ..$ : chr [1:22] "analytic_gflags" ...
.. ..$ : chr [1:8] "analytic_granule_pnt" ...
names(data$item_types)
[1] "_links" "display_description"
[3] "display_name" "id"
[5] "supported_asset_types"
data$item_types$id # Desired output should be same as Python output above
Thanks!
Upvotes: 0
Views: 346
Reputation: 107687
In essence, jq
is a separately installed command line module that can extract or transform JSON data after the curl
retrieval or any JSON not limited to curl
. Therefore, it does not do any more work than R's httr
call. See GitHub app page.
To avoid uneeded API information, simply extract needed item from return object by name chaining:
data = fromJSON(rawToChar(res$content))$item_types$id
data = fromJSON(rawToChar(res[["content"]]))[["item_types"]][["id"]]
Upvotes: 1