Hi there!
I'm trying to read a JSON string from an API and turn it into a data frame. The first part is easily done, as follows:
SETTING UP THE BASE OF MY API CALL
base_url <- "https://dadosabertos.camara.leg.br/api/v2"
path_proposicoes <- "/proposicoes?siglaTipo=PEC%2C%20PDC%2C%20PL%2C%20PLP%2C%20PDL%2C%20PRC"
path_ano <- "&ano=2023"
path_itens <- "&itens=100&ordem=ASC&ordenarPor=id"
path_pagina <- "&pagina="
i <- 1
api_url <- paste0(base_url, path_proposicoes, path_ano, path_itens, path_pagina, i)
SO I GET THE FOLLOWING URL:
But the API is paginated, so I'm generating a loop to get all pages:
dados <- NULL
while (i <= 7) {
print("Próxima página encontrada, baixando...")
api_url <- paste0(base_url, path_proposicoes, path_ano, path_itens, path_pagina, i)
raw_data <- GET(api_url)
texto <- content(raw_data, "text", encoding = "UTF-8")
json <- fromJSON(texto, flatten = TRUE)
df <- as.data.frame(json)
i <- i + 1
dados <- rbind(dados, df)
}
tabela <- as.data.frame(dados)
However, there is a secondary API call that will bring me complementary information for each row. The url for the API call is within the first data frame, in a column called "dados.uri". So I just need to include a secondary call for each row, correct? But here's where the problem starts.
j <- 1
tabela$dados.uri[j]
dados_proposicoes <- NULL
while (j <= 637) { ### Number of results from my paginated call
print("Dados encontrados, baixando...")
raw_data_proposicoes <- GET(tabela$dados.uri[j])
texto_proposicoes <- content(raw_data_proposicoes, "text", encoding = "UTF-8")
json_proposicoes <- fromJSON(texto_proposicoes, flatten = TRUE)
j <- j+1
}
df_proposicoes <- as.data.frame(json_proposicoes)
Everything's running smoothly and I get a JSON file, but I'm not able to turn it into a data frame, I get the following error:
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1, 0
Basically, what I need is to make a complementary call from my primary data frame and include the new results as new columns in each row of the first data frame.
Does anyone have any idea on how to do that?