Skip to contents

zujson converts between JSON and ordinary R vectors and lists, using vendored yyjson sources, so no system JSON library is required. It exists to serve zuhttp: parsing a response body and building a request body are the two things it is designed around, which makes it deliberately narrower than jsonlite, with a type mapping that is fully documented.

Installation

Install the released version from CRAN:

Or the development version from GitHub:

# install.packages("pak")
pak::pak("pedrobtz/zujson")

Usage

json_parse() turns JSON into R objects. An array becomes an atomic vector when its elements agree on a type, and a list when they do not — by default the type is never coerced away, though simplify = "coerce" opts into R’s own promotion when you would rather have the vector.

library(zujson)

json_parse('{"ok": true, "ids": [1, 2, 3]}')
#> $ok
#> [1] TRUE
#> 
#> $ids
#> [1] 1 2 3

json_write() goes the other way. A fully named vector or list becomes an object, anything else becomes an array.

json_write(list(query = "cats", limit = 10L))
#> [1] "{\"query\":\"cats\",\"limit\":10}"

A response body arrives as raw bytes and a request body should leave as raw bytes, so both directions take and return them without a detour through a string:

json_write_raw(list(q = "cats"))
#>  [1] 7b 22 71 22 3a 22 63 61 74 73 22 7d

json_parse(json_write_raw(list(q = "cats")))
#> $q
#> [1] "cats"

json_parse_ndjson() and json_write_ndjson() handle application/x-ndjson; json_parse_file(), json_parse_raw(), json_validate() and json_write_ndjson_raw() round out the set. The getting started article carries the full type mapping in both directions, the safety limits, and what the package deliberately does not do.

Testing

zujson’s tests cover its type mapping, errors and round trips, and run on every pull request across five platform and R version combinations. Differential tests use jsonlite as an external oracle, while JSONTestSuite checks 95/95 required acceptances and 188/188 required rejections. Sanitizer and fuzzing jobs exercise the C boundary and cleanup paths. See Testing and conformance for the test map, full results and intentional divergences.