Reputation: 656
I have data being pulled from an API call that looks like this. Please keep in mind that 99% of the data below in the dput
here is useless, I just need it to make this problem reproducible. Running it should return a response that looks like this:
[[1]]
Response [https://test.api.com/v4/12345/information]
Date: 2021-09-02 20:24
Status: 204
Content-Type: <unknown>
<EMPTY BODY>
[[2]]
Response [https://test.api.com/v4/23456/information]
Date: 2021-09-02 20:24
Status: 204
Content-Type: <unknown>
<EMPTY BODY>
What I am trying to do is extract information from these responses that looks like this:
url | date | status | |
---|---|---|---|
https://test.api.com/v4/12345/information | 9/2/21 20:24 | 204 | |
https://test.api.com/v4/23456/information | 9/2/21 20:24 | 204 |
If it were just one response, I could call this data by doing things like response_blurb$url
and response_blurb$date
but with multiple responses, I cannot do that.
dput is below:
response_blurb <- list(structure(list(url = "https://test.api.com/v4/12345/information",
status_code = 204L, headers = structure(list(`cache-control` = "no-cache",
pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000",
`x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block",
date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva",
`x-iinfo` = "7-62735819-62735821 NNNY CT(32 73 0) RT(1630614282821 31) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive",
"list")), all_headers = list(list(status = 204L, version = "HTTP/1.1",
headers = structure(list(`cache-control` = "no-cache",
pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000",
`x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block",
date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva",
`x-iinfo` = "7-62735819-62735821 NNNY CT(32 73 0) RT(1630614282821 31) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive",
"list")))), cookies = structure(list(domain = c("#HttpOnly_.secure",
".secure", ".secure", "#HttpOnly_api.secure"
), flag = c(TRUE, TRUE, TRUE, FALSE), path = c("/", "/",
"/", "/"), secure = c(TRUE, TRUE, TRUE, FALSE), expiration = structure(c(1661415232,
Inf, Inf, Inf), class = c("POSIXct", "POSIXt")), name = c("visid_incap_754428",
"nlbi_754428", "incap_ses_1286_754428", "ASP.NET_SessionId"
), value = c("kpT+BJeYSuGXS5d9taQHzJiYJmEAAAAAQUIPAAAAAAAyrXukfkBb9mpZHnGcr6zX",
"pprNEnQFZjMz5rnaoK07BAAAAADZLQdRn3tbElZnp5AzMYef", "uTPqYDW0LgRsKI+vbsrYEW0xMWEAAAAAkinWhXc+BLr4Y9HRlIvAtQ==",
"5ndj041zg1zda2us2ai3eape")), row.names = c(NA, -4L), class = "data.frame"),
content = raw(0), date = structure(1630614283, class = c("POSIXct",
"POSIXt"), tzone = "GMT"), times = c(redirect = 0, namelookup = 0.052203,
connect = 0.068757, pretransfer = 0.094581, starttransfer = 0,
total = 0.193515), request = structure(list(method = "POST",
url = "https://test.api.com/v4/12345/information",
headers = c(Accept = "application/json", Authorization = "Basic KFOSJFBfoepsEOTPSDnrfjs395031==",
`Content-Type` = "application/json"), fields = NULL,
options = list(useragent = "libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2",
post = TRUE, postfieldsize = 157L, postfields = as.raw(c(0x20
))), auth_token = NULL, output = structure(list(), class = c("write_memory",
"write_function"))), class = "request"), handle = '<pointer: 0x7ffb26c10060>'), class = "response"),
structure(list(url = "https://test.api.com/v4/23456/information",
status_code = 204L, headers = structure(list(`cache-control` = "no-cache",
pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000",
`x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block",
date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva",
`x-iinfo` = "7-62735819-62735821 SNNy RT(1630614282821 130) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive",
"list")), all_headers = list(list(status = 204L, version = "HTTP/1.1",
headers = structure(list(`cache-control` = "no-cache",
pragma = "no-cache", expires = "-1", `strict-transport-security` = "max-age=31536000",
`x-content-type-options` = "nosniff", `x-xss-protection` = "1; mode=block",
date = "Thu, 02 Sep 2021 20:24:43 GMT", `x-cdn` = "Imperva",
`x-iinfo` = "7-62735819-62735821 SNNy RT(1630614282821 130) q(0 0 0 -1) r(1 1) U6"), class = c("insensitive",
"list")))), cookies = structure(list(domain = c("#HttpOnly_.secure",
".secure", ".secure", "#HttpOnly_api.secure"
), flag = c(TRUE, TRUE, TRUE, FALSE), path = c("/", "/",
"/", "/"), secure = c(TRUE, TRUE, TRUE, FALSE), expiration = structure(c(1661415232,
Inf, Inf, Inf), class = c("POSIXct", "POSIXt")), name = c("visid_incap_754428",
"nlbi_754428", "incap_ses_1286_754428", "ASP.NET_SessionId"
), value = c("kpT+BJeYSuGXS5d9taQHzJiYJmEAAAAAQUIPAAAAAAAyrXukfkBb9mpZHnGcr6zX",
"pprNEnQFZjMz5rnaoK07BAAAAADZLQdRn3tbElZnp5AzMYef", "uTPqYDW0LgRsKI+vbsrYEW0xMWEAAAAAkinWhXc+BLr4Y9HRlIvAtQ==",
"5ndj041zg1zda2us2ai3eape")), row.names = c(NA, -4L), class = "data.frame"),
content = raw(0), date = structure(1630614283, class = c("POSIXct",
"POSIXt"), tzone = "GMT"), times = c(redirect = 0, namelookup = 3.8e-05,
connect = 4e-05, pretransfer = 0.000118, starttransfer = 0,
total = 0.072601), request = structure(list(method = "POST",
url = "https://test.api.com/v4/23456/information",
headers = c(Accept = "application/json", Authorization = "Basic KFOSJFBfoepsEOTPSDnrfjs395031==",
`Content-Type` = "application/json"), fields = NULL,
options = list(useragent = "libcurl/7.64.1 r-curl/4.3.2 httr/1.4.2",
post = TRUE, postfieldsize = 157L, postfields = as.raw(c(0x20
))), auth_token = NULL,
output = structure(list(), class = c("write_memory",
"write_function"))), class = "request"), handle = '<pointer: 0x7ffb26c10060>'), class = "response"))
Upvotes: 2
Views: 494
Reputation: 16998
You could try this:
library(purrr)
library(magrittr)
response_blurb %>%
map_df(~.x %>%
extract(c("url", "date", "status_code")))
which returns
# A tibble: 2 x 3
url date status_code
<chr> <dttm> <int>
1 https://test.api.com/v4/12345/information 2021-09-02 20:24:43 204
2 https://test.api.com/v4/23456/information 2021-09-02 20:24:43 204
Edit Thanks to nniloc's comment, this can be shortened to
response_blurb %>%
map_df(extract, c("url", "date", "status_code"))
Upvotes: 1
Reputation: 4243
As you mention you can run response_blurb$url
for one burb
, but for a list of blurb
s you would have to do response_blurb[[1]]$url
.
This can be expanded to capture the three variables you want response_blurb[[1]][c('url', 'date', 'status_code')]
. And this can be iterated over [[1]]
and [[2]]
using sapply
.
Bring it all together
sapply(response_blurb, '[', c('url', 'date', 'status_code'))
or to return a dataframe
data.frame(t(sapply(response_blurb, '[', c('url', 'date', 'status_code'))))
#-----
url date status_code
1 https://test.api.com/v4/12345/information 1630614283 204
2 https://test.api.com/v4/23456/information 1630614283 204
Upvotes: 1