Skip to content

Instantly share code, notes, and snippets.

@matthewfeickert
Last active December 11, 2020 04:40
Show Gist options
  • Save matthewfeickert/7d51c57dfde341e392b2521bc16cdd2d to your computer and use it in GitHub Desktop.
Save matthewfeickert/7d51c57dfde341e392b2521bc16cdd2d to your computer and use it in GitHub Desktop.
Interacting with JSON in R for Lauren

Writing and Reading JSON with jsonlite

Using the jsonlite library in R you can read, write, and manipulate JSON data.

This Gist contains an example (write_json.R) of writing a list containing ecological "data types" and arrays of column data types to a JSON file using jsonlite::write_json.

That JSON file nicely serializes a mapping of the key values (e.g., "FSH", "IHI") — the ecological data types — to the associated data — the column data types.

To use this serialization (a data structure written to file) an example is given (read_json.R) in which it is read into a data frame using jsonlite::read_json. It can then be manipulated as normal. N.B.: jsonlite::read_json can read either a file on disk or a file on a remote site if a URL is passed to it.

Running

These examples can be run from the command line using Rscript with the following commands

$ Rscript write_json.R
Wrote file column_schemas.json to disk

followed by

$ Rscript read_json.R
[1] "The first column data type in the FSH data type is: text"

These can of course be run in RStudio as well by loading them in and then executing them in the IDE.

{
"FSH": ["text", "text", "text", "date", "date", "text", "text", "text", "numeric", "numeric", "text", "text", "text", "text", "text", "text", "text", "text", "text", "text"],
"IHI": ["text", "text", "date", "skip", "skip", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "text", "text", "text", "text", "text", "text", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric"]
}
library(styler)
styler::style_dir(".")
library(jsonlite)
column_types <- jsonlite::read_json("column_schemas.json")
fish_columns <- column_types$FSH
print(paste("The first column data type in the FSH data type is:", fish_columns[1]))
library(jsonlite)
column_types <- list(
FSH = c(
"text", "text", "text", "date", "date", "text", "text", "text", "numeric",
"numeric", "text", "text", "text", "text", "text", "text", "text", "text",
"text", "text"
),
IHI = c(
"text", "text", "date", "skip", "skip", "numeric", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric",
"text", "text", "text", "text", "text", "text", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric", "numeric", "numeric",
"numeric", "numeric", "numeric", "numeric"
)
)
output_filename <- "column_schemas.json"
jsonlite::write_json(column_types, output_filename, pretty = TRUE)
cat("Wrote file", output_filename, "to disk\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment