R/get_json.R

Defines functions download_value get_value

get_value <- function(path, singleton = FALSE, verbose=TRUE){
  if (verbose){
    message("Retrieving ", path)
  }
  res <- jsonlite::read_json(path, simplifyVector = TRUE)

  if (singleton){
    res
  } else {
    res$value
  }
}

download_value <- function( path
                          , output_file
                          , empty_selection
                          , sep = ","
                          , progress_cb = invisible
                          , verbose = TRUE
                          ){
  if (verbose){
    message("Retrieving ", path)
  }
#
  progress_cb(NULL)

  res <- jsonlite::read_json(path, simplifyVector = TRUE)
  if (!is.data.frame(res$value)){
    res$value <- empty_selection
  }
  data.table::fwrite( res$value, output_file
                    , row.names = FALSE
                    , na = ""
#                    , quote = FALSE
                    , sep = sep
                    )

  progress_cb(res$value)

  path <- res[["@odata.nextLink"]]
  while(!is.null(path)){
    if (verbose){
      message("Retrieving ", path)
    }
    res <- jsonlite::read_json(path, simplifyVector = TRUE)
    data.table::fwrite( res$value
               , output_file
               , row.names = FALSE
               , na        = ""
#               , quote     = FALSE
               , sep       = sep
               , col.names = FALSE
               , append    = TRUE
               )
    progress_cb(res$value)
    path <- res[["@odata.nextLink"]]
  }
}
statistiekcbs/cbsccb documentation built on April 8, 2022, 2:38 a.m.