Fetching APIs in R

Hi,
I am using an external application SurveyCTO for data collection purpose. The data collected has to come to R through API. But I am unable to fetch the API. In the reprex, I have not put the username and password. But when I gave the username and password, it does not work. The code runs, but I get error. I have attached the screenshot of the data frame. How can I resolve this?

library(tidyverse)
library(httr)
#> Warning: package 'httr' was built under R version 4.1.3
#API Request with digest authentication
request <-
  GET("https://earlyspark.surveycto.com/api/v2/forms/data/wide/json/EarlySpark_Baseline_Assessment_2022_23?date=0",
      authenticate("username","password"))

#retrieve the contents of a request as a character vector
data_text <- content(request, "text")

#convert from JSON data to R object
data <- fromJSON(data_text, flatten = TRUE)
#> Error in fromJSON(data_text, flatten = TRUE): could not find function "fromJSON"
Created on 2022-10-14 by the reprex package (v2.0.1)

Hello,

Your error indicates that the function does not exist in your environment. Have you loaded jsonlite (since this is were fromJSON() lives)?

Kind regards

Oh yeah. I actually am able to run the code now after loading jsonlite. But there is error in the data when I view it. I have attached the screenshot and the modified reprex.

library(tidyverse)
library(httr)
#> Warning: package 'httr' was built under R version 4.1.3
library(jsonlite)
#> Warning: package 'jsonlite' was built under R version 4.1.3
#> 
#> Attaching package: 'jsonlite'
#> The following object is masked from 'package:purrr':
#> 
#>     flatten
#API Request with digest authentication
request <-GET("https://earlyspark.surveycto.com/api/v2/forms/data/wide/json/EarlySpark_Baseline_Assessment_2022_23?date=0",
      authenticate("username", "password"))

#retrieve the contents of a request as a character vector
data_text <- content(request, "text")

#convert from JSON data to R object
data <- fromJSON(data_text, flatten = TRUE)
Created on 2022-10-14 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.