Skip to content

Instantly share code, notes, and snippets.

@matt-dray
Created June 20, 2024 13:16
Show Gist options
  • Save matt-dray/01188d02f57ec3bf77de3d7e7e1fa4c5 to your computer and use it in GitHub Desktop.
Save matt-dray/01188d02f57ec3bf77de3d7e7e1fa4c5 to your computer and use it in GitHub Desktop.
Sketch for function in the style of `AzureStor::storage_read_*()` but for json/json.gz
#' Convenience Function: Read a (Zipped) JSON File from an Azure Container
#'
#' @param container A blob_container/storage_container object. Probably
#' generated by [connect_to_container].
#' @param file Character. Path to file within the container named by
#' `container`.
#'
#' @return A list.
#' @export
#'
#' @examples
#' \dontrun{get_container() |> read_json("file.json.gz")}
storage_read_json <- function(container, file) {
is_json <- tools::file_ext(file) == "json"
is_json_zip <- grepl("\\.json\\.gz$", file)
if (!all(is_json, is_json_zip)) {
stop("Argument 'file' must have extension json or json.gz", call. = FALSE)
}
file_download <-
AzureStor::storage_download(container, src = file, dest = NULL)
if (is_json) jsonlite::read_json(file_download, simplifyVector = TRUE)
if (is_json_zip) jsonlite::parse_gzjson_raw(file_download, simplifyVector = TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment