Created
June 20, 2024 13:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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