Parse the body of a response with httr2::resp_body_json() and optionally
extract a named subset of that body.
Arguments
- resp
(
httr2_response) A singlehttr2::response()object (as returned byhttr2::req_perform()).- subset_path
(
character) An optional vector indicating the path to the "real" object within the body ofresp. For example, many APIs return a body with information about the status of the response, cache information, perhaps pagination information, and then the actual data in a field such asdata. If the desired part of the response body is indata$objects, the value of this argument should bec("data", "object").- simplifyVector
Should JSON arrays containing only primitives (i.e. booleans, numbers, and strings) be caused to atomic vectors?
See also
Other opinionated response parsers:
req_tidy_policy(),
resp_tidy(),
resp_tidy_json_tibblify(),
resp_tidy_unknown(),
tidy_policy_body_auto(),
tidy_policy_json(),
tidy_policy_json_tibblify(),
tidy_policy_prepare(),
tidy_policy_unknown()
Examples
resp <- httr2::response_json(
body = list(list(id = 1, name = "Alice"), list(id = 2, name = "Bob"))
)
resp_tidy_json(resp)
#> [[1]]
#> [[1]]$id
#> [1] 1
#>
#> [[1]]$name
#> [1] "Alice"
#>
#>
#> [[2]]
#> [[2]]$id
#> [1] 2
#>
#> [[2]]$name
#> [1] "Bob"
#>
#>
# Extract a nested subset of the response body
resp_nested <- httr2::response_json(
body = list(data = list(list(id = 1), list(id = 2)))
)
resp_tidy_json(resp_nested, subset_path = "data")
#> [[1]]
#> [[1]]$id
#> [1] 1
#>
#>
#> [[2]]
#> [[2]]$id
#> [1] 2
#>
#>