R/read_json.R

Defines functions read_json

Documented in read_json

#' Reads JSON from an input uri (file, url, ...) and returns a
#' \code{\link{tbl_json}} object
#'
#' @param path to some json data
#' @param format
#'   If \code{"json"}, process the data like one large JSON record.
#'   If \code{"jsonl"}, process the data one JSON record per line (json lines
#'   format).
#'   If \code{"infer"}, the format is the suffix of the given filepath.
#' @return a \code{\link{tbl_json}} object
#' @export
read_json <- function(path, format = c("json", "jsonl", "infer")) {

  if (length(format) > 1 || identical(format,"infer")) {
    format <- tail(strsplit(path, "[.]")[[1]], 1)
  }
  if (format == "json") {
    data <- readChar(path, nchars = file.info(path)$size)
  } else if (format == "jsonl") {
    data <- readLines(path)
  } else {
    stop(sprintf("Unrecognized json format: %s", format))
  }
  data %>% as.tbl_json
}

Try the tidyjson package in your browser

Any scripts or data that you put into this service are public.

tidyjson documentation built on Jan. 7, 2023, 1:14 a.m.