CYBER WEEK - EXTRA SAVINGS EVENT
TRY A FREE LESSON

API mission answers in r answers outdated. Unable to "complete" course

Screen Link:
https://app.dataquest.io/m/554/working-with-apis-in-r/6/converting-json-into-dataframe

My Code:

library(jsonlite)
wb_api_json_get_df <- function(endpoint) {
  
  # Preparing the URL 
  url <- modify_url("http://api.worldbank.org", path = endpoint)
  
  # API requests
  response <- GET(url)

  # Tracking errors
  if ( http_error(response) ){
    print(status_code(response))
    stop("Something went wrong.", call. = FALSE)
  }
    
  if (http_type(response) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }
  
  # Extracting content
  json_text <- content(response, "text")
    
  # TODO 1: add code to convert the extracted content into a dataframe

  # TODO 2: modify to return the dataframe 
  json_text
}

# Type the answer to the second instruction below
wb_api_json_get_df <- function(endpoint) {
  
  # Preparing the URL 
  url <- modify_url("http://api.worldbank.org", path = endpoint)
  
  # API requests
  response<-GET(url)
  
  #tracking status codes
  if(http_error(response)){
    print(status_code(response))
    stop("Something went wrong.", call. = FALSE)
  }
  
  if(http_type(response)!="application/json"){
    stop("API did not return json", call. = FALSE)
  } 
  
  #Get content as text
  json_text<- content(response, as="text")
  dataframe<-fromJSON(json_text)
  return(dataframe)
  
}

wb_indicator_json_df<-wb_api_json_get_df("v2/indicator?format=json")

What I expected to happen:
I used the same code as in the answers but it still gave me an error code.
This is an API topic, so obviously the datasource has been updated since this course was created. My results have additional columns and pages, but the course “expected” answers have less as they were an output of running the APi request a while ago, when the data was less.

What actually happened:

 wb_indicator_json_dflist (list)
- actual + expected

  [[1]]
  [[1]]$page
  [1] 1

  [[1]]$pages
- [1] 355
+ [1] 351
  
  [[1]]$per_page
  [1] "50"

  [[1]]$total
- [1] 17748
+ [1] 17517

This means that even when i finish the missions correctly, i still do not get the “completed” status. Which means i will miss out on the certificates.

Please help

1 Like

My code is very close (not using return in the function). When I ‘Run the Code’, all good with the text returned in the variable “wb_indicator_json_text” .

However, when I ‘Submit the Answer’, it indicates an error but does not indicate what the error is. Hmmm, suspect you are correct that the data has changed.

Comments invited to resolve.

I’m having the same problem. Hopefully this will be corrected soon