Search code examples
rdataframejsonliterjson

Automatic upload json files via API using R


I want take financial data using API. I do so.

#load jsons
library("rjson")
json_file <- "https://api.coindesk.com/v1/bpi/currentprice/USD.json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

#get json content as data.frame
x = data.frame(json_data$time$updated,json_data$time$updatedISO,json_data$time$updateduk,json_data$bpi$USD)
x

But the main problem is that information changes every minute, so i can't gather history. Are there ways to make R independently connect every minute(i.e. in real time mode) to this site and collect data every minute. So collected data must be saved in C:/Myfolder. Is it possible to do it?


Solution

  • Something like this could do it

    library("rjson")
    json_file <- "https://api.coindesk.com/v1/bpi/currentprice/USD.json"
    
    numOfTimes <- 2L # how many times to run in total
    sleepTime <- 60L  # time to wait between iterations (in seconds)
    iteration <- 0L
    while (iteration < numOfTimes) {
      # gather data
      json_data <- fromJSON(paste(readLines(json_file), collapse=""))
      # get json content as data.frame
      x = data.frame(json_data$time$updated,json_data$time$updatedISO,json_data$time$updateduk,json_data$bpi$USD)
      # create file to save in 'C:/Myfolder' 
      # alternatively, create just one .csv file and update it in each iteration
      nameToSave <- nameToSave <- paste('C:/Myfolder/', 
                                         gsub('\\D','',format(Sys.time(),'%F%T')), 
                                        'json_data.csv', sep = '_')
      # save the file
      write.csv(x, nameToSave)
      # update counter and wait 
      iteration <- iteration + 1L
      Sys.sleep(sleepTime)
    }    
    

    Note that this requires to have an R session opened (you could create a .exe or .bat file and have it run in the background).