https://github.com/sckott/jqr
jq inspired json parsing
https://github.com/sckott/jqr
Last synced: over 1 year ago
JSON representation
jq inspired json parsing
- Host: GitHub
- URL: https://github.com/sckott/jqr
- Owner: sckott
- License: other
- Created: 2014-05-30T17:31:09.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-05-30T17:56:35.000Z (about 12 years ago)
- Last Synced: 2025-03-23T23:36:11.462Z (over 1 year ago)
- Language: R
- Homepage:
- Size: 125 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
jqr
========
A json parser in R inspired by [`jq`](http://stedolan.github.io/jq/).
### Quick start
Install
```coffee
devtools::install_github("sckott/jqr")
```
Functions
```coffee
# json parser to list
jq <- function(json, ...){
json %>% fromJSON(simplifyVector = FALSE, ...)
}
# extract named elements from list
`%v%` <- function(input, vars){
if(length(vars) > 1){
lapply(input, function(x) x[vars])
} else {
sapply(input, function(x) x[[vars]])
}
}
# Extract single node
`%s%` <- function(input, vars) input[[vars]]
# key-value, rename keys
kv <- function(x, ...){
args <- list(...)
toget <- unname(do.call(c, args))
lapply(x, function(z){
names(z) <- names(args)
z
})
}
```
Load some libraries
```coffee
library("jsonlite")
library("magrittr")
library("httr")
library("data.table")
```
Get some data
```coffee
json <- content(GET('http://api.plos.org/search/?q=*:*&wt=json'), as = "text")
```
Parse
_note how you can index multiple levels down (here, `response.docs`, would normally be object$response$docs) the list with `%s%`_
```coffee
json %>%
jq %s%
'response.docs' %v%
c('id','journal') %>%
rbindlist
```
```coffee
id journal
1: 10.1371/journal.pone.0075723/title PLoS ONE
2: 10.1371/journal.pone.0087935 PLoS ONE
3: 10.1371/journal.pone.0087935/title PLoS ONE
4: 10.1371/journal.pone.0087935/abstract PLoS ONE
5: 10.1371/journal.pone.0087935/references PLoS ONE
6: 10.1371/journal.pone.0087935/body PLoS ONE
7: 10.1371/journal.pone.0087935/introduction PLoS ONE
8: 10.1371/journal.pone.0087935/results_and_discussion PLoS ONE
9: 10.1371/journal.pone.0087935/materials_and_methods PLoS ONE
10: 10.1371/journal.pone.0087935/conclusions PLoS ONE
```