as_r {rjsoncons}R Documentation

Parse JSON or NDJSON to R

Description

as_r() transforms JSON or NDJSON to an R object.

Usage

as_r(
  data,
  object_names = "asis",
  ...,
  n_records = Inf,
  verbose = FALSE,
  data_type = j_data_type(data)
)

Arguments

data

a character() JSON string or NDJSON records, or the name of a file or URL containing JSON or NDJSON, or an R object parsed to a JSON string using jsonlite::toJSON().

object_names

character(1) order data object elements "asis" (default) or "sort" before filtering on path.

...

passed to jsonlite::toJSON when data is an R object.

n_records

numeric(1) maximum number of NDJSON records parsed.

verbose

logical(1) report progress when parsing large NDJSON files.

data_type

character(1) type of data; one of "json", "ndjson", or a value returned by j_data_type().

Details

The as = "R" argument to j_query(), j_pivot(), and the as_r() function transform JSON or NDJSON to an R object. JSON and NDJSON can be a character vector, file, or url, or an R object (which is first translated to a JSON string). Main rules are:

The vignette reiterates this information and provides additional details.

Value

as_r() returns an R object.

Examples

## as_r()
as_r('[1, 2, 3]')       # JSON integer array -> R integer vector
as_r('[1, 2.0, 3]')     # JSON intger and double array -> R numeric vector
as_r('[1, 2.0, "3"]')   # JSON mixed array -> R list
as_r('[1, 2147483648]') # JSON integer > R integer max -> R numeric vector

json <- '{"b": 1, "a": ["c", "d"], "e": true, "f": [true], "g": {}}'
as_r(json) |> str()     # parsing complex objects
identical(              # JSON scalar and length 1 array identical in R
    as_r('{"a": 1}'), as_r('{"a": [1]}')
)


[Package rjsoncons version 1.3.1 Index]