An open API service indexing awesome lists of open source software.

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.

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).