Learn R Programming

jsontools (version 0.1.0)

parse_json: Convert JSON to an R object

Description

A wrapper around the great jsonlite::parse_json. The differences are:

  • expose argument bigint_as_char with default TRUE.

  • control how to handle NA and NULL.

  • simplifyDataFrame, simplifyMatrix, and flatten default to FALSE as they are not very stable in many real world APIs. Use the tibblify package for a more robust conversion to a dataframe.

  • don't collapse strings but error instead if they have more than one element.

Usage

parse_json(
  x,
  .na = json_na_error(),
  .null = NULL,
  simplifyVector = TRUE,
  simplifyDataFrame = FALSE,
  simplifyMatrix = FALSE,
  flatten = FALSE,
  bigint_as_char = bigint_default(),
  ...
)

Arguments

x

a scalar JSON character

.na

Value to return if x is NA. By default an error of class jsontools_error_na_json is thrown.

.null

Return the prototype of .null if x is NULL or a zero length character

simplifyVector, simplifyDataFrame, simplifyMatrix, flatten, ...

passed on to jsonlite::parse_json.

bigint_as_char

Parse big integers as character? The option jsontools.bigint_as_char is used as default.

Value

A R object. The type depends on the input but is usually a list or a data frame.

Details

To parse a vector of JSON use parse_json_vector.

Examples

Run this code
# NOT RUN {
# Parse escaped unicode
parse_json('{"city" : "Z\\u00FCrich"}')

# big integers
big_num <- "9007199254740993"
as.character(parse_json(big_num, bigint_as_char = FALSE))
as.character(parse_json(big_num, bigint_as_char = TRUE))

# NA error by default
try(parse_json(NA))
# ... but one can specify a default value
parse_json(NA, .na = data.frame(a = 1, b = 2))

# input of size 0
parse_json(NULL)
parse_json(character(), .null = data.frame(a = 1, b = 2))
# }

Run the code above in your browser using DataLab