R/cat_json_str.R

Defines functions cat_json_str

Documented in cat_json_str

# ---------------------------------------------------------------------------- #
#' Concatenate stringified JSON
#'
#' Strips 'stringified' JSON of newlines and spaces following colons or commas.
#'
#' @param txt The string of stringified JSON to be concatenated.
#'
#' @returns A string (stringified JSON), stripped of any newlines and spaces
#'   following colons or commas.
#'
#' @examples
#' cat_json_str("{\n    'v1': [20],\n    'v2': [1.5]\n  }")
#'
#' @export
#'
cat_json_str <- function(txt) {
  pattern_repl <- c("\n *" = "", ": " = ":", ", +\"" = ",\"")
  stringr::str_replace_all(txt, pattern_repl)
}

# ---------------------------------------------------------------------------- #
toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.