rdrr.io
Open in
urlscan Pro
138.201.54.179
Public Scan
URL:
https://rdrr.io/cran/jsonlite/f/vignettes/json-aaquickstart.Rmd
Submission: On September 29 via manual from US — Scanned from DE
Submission: On September 29 via manual from US — Scanned from DE
Form analysis
2 forms found in the DOMGET /search
<form class="item" method="GET" action="/search">
<div class="ui right action input">
<input type="text" placeholder="packages, doc text, code..." size="24" name="q">
<button type="submit" class="ui green icon button"><i class="search icon"></i></button>
</div>
</form>
GET /search
<form class="item" method="GET" action="/search">
<div class="sub header" style="margin-bottom: 4px">Search the jsonlite package</div>
<div class="ui action input" style="padding-right: 32px">
<input type="hidden" name="package" value="jsonlite">
<input type="hidden" name="repo" value="cran">
<input type="text" placeholder="" name="q">
<button type="submit" class="ui green icon button">
<i class="search icon"></i>
</button>
</div>
</form>
Text Content
rdrr.io Find an R package R language docs Run R in your browser JSONLITE A SIMPLE AND ROBUST JSON PARSER AND GENERATOR FOR R Package index Search the jsonlite package Vignettes * Combining pages of JSON data with jsonlite * Fetching JSON data from REST APIs * Getting started with JSON and jsonlite Functions 117 Source code 100 Man pages 11 * base64: Encode/decode base64 * flatten: Flatten nested data frames * fromJSON: Convert R objects to/from JSON * gzjson: Gzipped JSON * prettify: Prettify or minify a JSON string * rbind_pages: Combine pages into a single data frame * read_json: Read/write JSON * serializeJSON: serialize R objects to JSON * stream_in: Streaming JSON input/output * unbox: Unbox a vector or data frame * validate: Validate JSON * Browse all... Home / CRAN / jsonlite / Getting started with JSON and jsonlite Modernize your database with MongoDB Atlas, the leading developer data platform. Ads by EthicalAds GETTING STARTED WITH JSON AND JSONLITE IN JSONLITE: A SIMPLE AND ROBUST JSON PARSER AND GENERATOR FOR R library(knitr) opts_chunk$set(comment="") #this replaces tabs by spaces because latex-verbatim doesn't like tabs #no longer needed because yajl does not use tabs. #toJSON <- function(...){ # gsub("\t", " ", jsonlite::toJSON(...), fixed=TRUE); #} GETTING STARTED WITH JSON AND JSONLITE The jsonlite package is a JSON parser/generator optimized for the web. Its main strength is that it implements a bidirectional mapping between JSON data and the most important R data types. Thereby we can convert between R objects and JSON without loss of type or information, and without the need for any manual data munging. This is ideal for interacting with web APIs, or to build pipelines where data structures seamlessly flow in and out of R using JSON. library(jsonlite) all.equal(mtcars, fromJSON(toJSON(mtcars))) This vignette introduces basic concepts to get started with jsonlite. For a more detailed outline and motivation of the mapping, see: arXiv:1403.2805. SIMPLIFICATION Simplification is the process where JSON arrays automatically get converted from a list into a more specific R class. The fromJSON function has 3 arguments which control the simplification process: simplifyVector, simplifyDataFrame and simplifyMatrix. Each one is enabled by default. | JSON structure | Example JSON data | Simplifies to R class | Argument in fromJSON | | ----------------------|----------------------------------------------------------|-----------------------|----------------------| | Array of primitives | ["Amsterdam", "Rotterdam", "Utrecht", "Den Haag"] | Atomic Vector | simplifyVector | | Array of objects | [{"name":"Erik", "age":43}, {"name":"Anna", "age":32}] | Data Frame | simplifyDataFrame | | Array of arrays | [ [1, 2, 3], [4, 5, 6] ] | Matrix | simplifyMatrix | ATOMIC VECTORS When simplifyVector is enabled, JSON arrays containing primitives (strings, numbers, booleans or null) simplify into an atomic vector: # A JSON array of primitives json <- '["Mario", "Peach", null, "Bowser"]' # Simplifies into an atomic vector fromJSON(json) Without simplification, any JSON array turns into a list: # No simplification: fromJSON(json, simplifyVector = FALSE) DATA FRAMES When simplifyDataFrame is enabled, JSON arrays containing objects (key-value pairs) simplify into a data frame: json <- '[ {"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"}, {"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"}, {}, {"Name" : "Bowser", "Occupation" : "Koopa"} ]' mydf <- fromJSON(json) mydf The data frame gets converted back into the original JSON structure by toJSON (whitespace and line breaks are ignorable in JSON). mydf$Ranking <- c(3, 1, 2, 4) toJSON(mydf, pretty=TRUE) Hence you can go back and forth between dataframes and JSON, without any manual data restructuring. MATRICES AND ARRAYS When simplifyMatrix is enabled, JSON arrays containing equal-length sub-arrays simplify into a matrix (or higher order R array): json <- '[ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ]' mymatrix <- fromJSON(json) mymatrix Again, we can use toJSON to convert the matrix or array back into the original JSON structure: toJSON(mymatrix, pretty = TRUE) The simplification works for arrays of arbitrary dimensionality, as long as the dimensions match (R does not support ragged arrays). json <- '[ [[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]] ]' myarray <- fromJSON(json) myarray[1, , ] myarray[ , ,1] This is all there is to it! For a more detailed outline and motivation of the mapping, see: arXiv:1403.2805. TRY THE JSONLITE PACKAGE IN YOUR BROWSER library(jsonlite) help(jsonlite) Run (Ctrl-Enter) Any scripts or data that you put into this service are public. Nothing jsonlite documentation built on July 9, 2023, 6:11 p.m. R PACKAGE DOCUMENTATION rdrr.io home R language documentation Run R code online BROWSE R PACKAGES CRAN packages Bioconductor packages R-Forge packages GitHub packages WE WANT YOUR FEEDBACK! Note that we can't provide technical support on individual packages. You should contact the package authors for that. Tweet to @rdrrHQ GitHub issue tracker ian@mutexlabs.com Personal blog What can we improve? The page or its content looks wrong I can't find what I'm looking for I have a suggestion Other Extra info (optional) Submit Improve this page Embedding an R snippet on your website Add the following code to your website. REMOVE THIS Copy to clipboard For more information on customizing the embed code, read Embedding Snippets. Close