https://github.com/blmoore/rjsonpath
JSONPath for R
https://github.com/blmoore/rjsonpath
json jsonpath r rstats
Last synced: 9 months ago
JSON representation
JSONPath for R
- Host: GitHub
- URL: https://github.com/blmoore/rjsonpath
- Owner: blmoore
- License: mit
- Created: 2017-05-16T06:24:08.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-03T22:35:33.000Z (almost 9 years ago)
- Last Synced: 2025-03-30T14:11:37.228Z (12 months ago)
- Topics: json, jsonpath, r, rstats
- Language: R
- Homepage: http://blm.io/rjsonpath
- Size: 48.8 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# rjsonpath
[](https://travis-ci.org/blmoore/rjsonpath)
[](https://codecov.io/gh/blmoore/rjsonpath)
[](http://blm.io/rjsonpath)
[](https://cran.r-project.org/package=rjsonpath)
Reading JSON into R usually leaves you with some deeply-nested
list objects which are a pain to munge and navigate — applying path
expressions is one way to make this easier. `rjsonpath` implements
[JSONPath](http://goessner.net/articles/JsonPath/), a
selector / querying language for JSON, similar to XPath for
XML.
## Install
Install from github with:
```{r eval=FALSE}
devtools::install_github("blmoore/rjsonpath")
```
## Usage
As an example, take this simple JSON:
```{javascript}
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
```
```{r write_json_object, echo = FALSE}
json <- '{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}'
json <- RJSONIO::fromJSON(json)
library(rjsonpath)
```
Via `read_json` this can be read into R as:
```{r}
json
```
Pretty horrible right? To gather all the `onclick` methods
into a vector with base R you might write:
```{r}
sapply(json$menu$popup$menuitem, `[[`, "onclick")
```
Using `rjsonpath` this could instead be:
```{r, message=FALSE}
json_path(json, "$.menu.popup.menuitem[*].onclick")
```
Or even just:
```{r, message=FALSE}
json_path(json, "$..onclick")
```
For more more complex examples, see [below](#Advanced expressions).
## Advanced expressions