Access directly to values and array elements in a JSON string.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
R
inst
man
vignettes
.Rbuildignore
.gitignore
BinaryFiles
DESCRIPTION
LICENSE
NAMESPACE
README.md
jsonAccess.Rproj

README.md

The jsonAccess package

library(jsonAccess)

Assume you have the following JSON string:

> json <- "{\"a\": {\"b\": 8, \"c\": [2, [99, \"x\"]]}, \"b\": [10.14, 11.618]}"
  • Prettify:
> jsonPretty(json) %>% cat
## {
##   "a": {
##     "b": 8,
##     "c": [
##       2,
##       [
##         99,
##         "x"
##       ]
##     ]
##   },
##   "b": [
##     10.14,
##     11.618
##   ]
## }
  • Access to the value of the key "a":
> jsonAccess(json, "a")
## [1] "{\"b\":8,\"c\":[2,[99,\"x\"]]}"
  • Try to access to a key which is not there:
> jsonAccess(json, "z")
## [1] "null"
  • Access to the value of the key "b" in the value of the key "a":
> jsonAccess(json, c("a","b"))
## [1] "8"
  • Acces to the array in key "c":
> ( Array <- jsonAccess(json, c("a","c")) )
## [1] "[2,[99,\"x\"]]"
  • Get its 1-th element:
> jsonElemAt(Array, 1)
## [1] "[99,\"x\"]"
  • Get an element only if it is a number:
> jsonElemAt_ifNumber(Array, 0)
## [1] "2"
> jsonElemAt_ifNumber(Array, 1)
## [1] "null"
  • Chain:
> jsonAccess(json, c("a","c")) %>% jsonElemAt(1) %>% jsonElemAt(1)
## [1] "\"x\""
  • Get the members as an array of pairs:
> jsonMembers(json) %>% jsonPretty %>% cat
## [
##   [
##     "a",
##     {
##       "b": 8,
##       "c": [
##         2,
##         [
##           99,
##           "x"
##         ]
##       ]
##     }
##   ],
##   [
##     "b",
##     [
##       10.14,
##       11.618
##     ]
##   ]
## ]

This package includes DLLs generated by the Haskell library json-access.