https://github.com/stla/jsonaccess
Access directly to values and array elements in a JSON string.
https://github.com/stla/jsonaccess
json r
Last synced: 2 months ago
JSON representation
Access directly to values and array elements in a JSON string.
- Host: GitHub
- URL: https://github.com/stla/jsonaccess
- Owner: stla
- License: gpl-3.0
- Created: 2017-03-04T17:01:23.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-14T16:17:35.000Z (over 9 years ago)
- Last Synced: 2025-03-31T11:15:26.297Z (over 1 year ago)
- Topics: json, r
- Language: R
- Homepage:
- Size: 43 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# The jsonAccess package
```r
library(jsonAccess)
```
Assume you have the following JSON string:
```r
> json <- "{\"a\": {\"b\": 8, \"c\": [2, [99, \"x\"]]}, \"b\": [10.14, 11.618]}"
```
- Prettify:
```r
> jsonPretty(json) %>% cat
## {
## "a": {
## "b": 8,
## "c": [
## 2,
## [
## 99,
## "x"
## ]
## ]
## },
## "b": [
## 10.14,
## 11.618
## ]
## }
```
- Access to the value of the key `"a"`:
```r
> jsonAccess(json, "a")
## [1] "{\"b\":8,\"c\":[2,[99,\"x\"]]}"
```
- Try to access to a key which is not there:
```r
> jsonAccess(json, "z")
## [1] "null"
```
- Access to the value of the key `"b"` in the value of the key `"a"`:
```r
> jsonAccess(json, c("a","b"))
## [1] "8"
```
- Acces to the array in key `"c"`:
```r
> ( Array <- jsonAccess(json, c("a","c")) )
## [1] "[2,[99,\"x\"]]"
```
- Get its 1-th element:
```r
> jsonElemAt(Array, 1)
## [1] "[99,\"x\"]"
```
- Get an element only if it is a number:
```r
> jsonElemAt_ifNumber(Array, 0)
## [1] "2"
> jsonElemAt_ifNumber(Array, 1)
## [1] "null"
```
- Chain:
```r
> jsonAccess(json, c("a","c")) %>% jsonElemAt(1) %>% jsonElemAt(1)
## [1] "\"x\""
```
- Get the members as an array of pairs:
```r
> 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](https://github.com/stla/json-access).