I am working on parsing data from PowerBI REST API for activity data. The way this API functions is - the same end point may return data with 10 fields today and tomorrow it may return with 15 fields. My goal is to run a scheduled process that would extract daily data (say into a SQL table). I predefined an output data frame with the columns that I need. But I am looking for a way to handle the case when - say I defined 12 columns in my output data frame and in today's REST API extract, the results did not contain 1 of those 12 columns. I would like to source them as NA (or an empty string). How to do that in R? Below is a block of code I am working with:
response<-httr::GET(url=RESTEndPoint,config=httpHeader)
parsedResp<-httr::content(response, "text",encoding = "UTF-
8")%>%jsonlite::fromJSON(flatten = TRUE)
df<-as.data.frame(parsedResp$activityEventEntities)
outputDF<-df %>%
dplyr::select(
LogID='Id'
,CreationTimeD='CreationTime'
,Operation='Operation'
,OrganizationID='OrganizationId'
)
Say if the field 'Operation' is missing from the parsed response, then this would throw the error: 'Error: Can't subset columns that don't exist.' since that's how dplyr:Select works. Is there a way to say, when the field 'Operation' is missing in the parsed response, assign it as NA and move on with the next iteration of the loop
Thank you!