forked from ropensci/jqr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
411 lines (294 loc) · 8.76 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
jqr
=======
```{r echo=FALSE}
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
warning = FALSE,
message = FALSE
)
```
[![R-CMD-check](https://github.com/ropensci/jqr/workflows/R-CMD-check/badge.svg)](https://github.com/ropensci/jqr/actions?query=workflow%3AR-CMD-check)
[![codecov](https://codecov.io/gh/ropensci/jqr/branch/master/graph/badge.svg)](https://codecov.io/gh/ropensci/jqr)
[![cran checks](https://cranchecks.info/badges/worst/jqr)](https://cranchecks.info/pkgs/jqr)
[![rstudio mirror downloads](https://cranlogs.r-pkg.org/badges/jqr?color=0DA6CD)](https://github.com/r-hub/cranlogs.app)
[![cran version](https://www.r-pkg.org/badges/version/jqr)](https://cran.r-project.org/package=jqr)
R interface to jq, a JSON processor http://stedolan.github.io/jq/
`jqr` makes it easy to process large amounts of json without having to
convert from json to R, or without using regular expressions. This
means that the eventual loading into R can be quicker.
- Introduction vignette at <https://cran.r-project.org/package=jqr>
## Quickstart Tutorial
The `jq` command line examples from the [jq tutorial](https://stedolan.github.io/jq/tutorial/) work exactly the same in R!
```{r}
library(curl)
library(jqr)
curl('https://api.github.com/repos/ropensci/jqr/commits?per_page=5') %>%
jq('.[] | {message: .commit.message, name: .commit.committer.name}')
```
Try running some of the [other examples](https://stedolan.github.io/jq/tutorial/).
## Installation
Binary packages for __OS-X__ or __Windows__ can be installed directly from CRAN:
```r
install.packages("jqr")
```
Installation from source on Linux or OSX requires [`libjq`](https://stedolan.github.io/jq/). On __Ubuntu 14.04 and 16.04 lower__ use [libjq-dev](https://launchpad.net/~cran/+archive/ubuntu/jq) from Launchpad:
```
sudo add-apt-repository -y ppa:cran/jq
sudo apt-get update -q
sudo apt-get install -y libjq-dev
```
More __recent Debian or Ubuntu__ install [libjq-dev](https://packages.debian.org/testing/libjq-dev) directly from Universe:
```
sudo apt-get install -y libjq-dev
```
On __Fedora__ we need [jq-devel](https://apps.fedoraproject.org/packages/jq-devel):
```
sudo yum install jq-devel
````
On __CentOS / RHEL__ we install [jq-devel](https://apps.fedoraproject.org/packages/jq-devel) via EPEL:
```
sudo yum install epel-release
sudo yum install jq-devel
```
On __OS-X__ use [jq](https://github.com/Homebrew/homebrew-core/blob/master/Formula/jq.rb) from Homebrew:
```
brew install jq
```
On __Solaris__ we can have [libjq_dev](https://www.opencsw.org/packages/libjq_dev) from [OpenCSW](https://www.opencsw.org/):
```
pkgadd -d http://get.opencsw.org/now
/opt/csw/bin/pkgutil -U
/opt/csw/bin/pkgutil -y -i libjq_dev
```
```{r}
library(jqr)
```
## Interfaces
### low level
There's a low level interface in which you can execute `jq` code just as you would on the command line:
```{r}
str <- '[{
"foo": 1,
"bar": 2
},
{
"foo": 3,
"bar": 4
},
{
"foo": 5,
"bar": 6
}]'
```
```{r}
jq(str, ".[]")
```
```{r}
jq(str, "[.[] | {name: .foo} | keys]")
```
Note that we print the output to look like a valid JSON object to make it
easier to look at. However, it's a simple character string or vector of strings.
A trick you can do is to wrap your jq program in brackets like `[.[]]` instead
of `.[]`, e.g.,
```{r}
jq(str, ".[]") %>% unclass
# vs.
jq(str, "[.[]]") %>% unclass
```
Combine many jq arguments - they are internally combined with a pipe ` | `
(note how these are identical)
```{r}
jq(str, ".[] | {name: .foo} | keys")
jq(str, ".[]", "{name: .foo}", "keys")
```
Also accepts many JSON inputs now
```{r}
jq("[123, 456] [77, 88, 99]", ".[]")
jq('{"foo": 77} {"bar": 45}', ".[]")
jq('[{"foo": 77, "stuff": "things"}] [{"bar": 45}] [{"n": 5}]', ".[] | keys")
# if you have jsons in a vector
jsons <- c('[{"foo": 77, "stuff": "things"}]', '[{"bar": 45}]', '[{"n": 5}]')
jq(paste0(jsons, collapse = " "), ".[]")
```
### high level
The other is higher level, and uses a suite of functions to construct queries. Queries are constucted, then excuted internally with `jq()` after the last piped command.
You don't have to use pipes though. See examples below.
Examples:
Index
```{r}
x <- '[{"message": "hello", "name": "jenn"}, {"message": "world", "name": "beth"}]'
x %>% index()
```
Sort
```{r}
'[8,3,null,6]' %>% sortj
```
reverse order
```{r}
'[1,2,3,4]' %>% reverse
```
Show the query to be used using `peek()`
```{r}
'[1,2,3,4]' %>% reverse %>% peek
```
#### get multiple outputs for array w/ > 1 element
```{r}
x <- '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
jq(x, '{user, title: .titles[]}')
x %>% index()
x %>% build_object(user, title = `.titles[]`)
jq(x, '{user, title: .titles[]}') %>% jsonlite::toJSON() %>% jsonlite::validate()
```
#### string operations
join
```{r}
'["a","b,c,d","e"]' %>% join
'["a","b,c,d","e"]' %>% join(`;`)
```
ltrimstr
```{r}
'["fo", "foo", "barfoo", "foobar", "afoo"]' %>% index() %>% ltrimstr(foo)
```
rtrimstr
```{r}
'["fo", "foo", "barfoo", "foobar", "foob"]' %>% index() %>% rtrimstr(foo)
```
startswith
```{r}
'["fo", "foo", "barfoo", "foobar", "barfoob"]' %>% index %>% startswith(foo)
'["fo", "foo"] ["barfoo", "foobar", "barfoob"]' %>% index %>% startswith(foo)
```
endswith
```{r}
'["fo", "foo", "barfoo", "foobar", "barfoob"]' %>% index %>% endswith(foo)
```
tojson, fromjson, tostring
```{r}
'[1, "foo", ["foo"]]' %>% index
'[1, "foo", ["foo"]]' %>% index %>% tostring
'[1, "foo", ["foo"]]' %>% index %>% tojson
'[1, "foo", ["foo"]]' %>% index %>% tojson %>% fromjson
```
contains
```{r}
'"foobar"' %>% contains("bar")
```
unique
```{r}
'[1,2,5,3,5,3,1,3]' %>% uniquej
```
#### filter
With filtering via `select()` you can use various operators, like `==`,
`&&`, `||`. We translate these internally for you to what `jq` wants
to see (`==`, `and`, `or`).
Simple, one condition
```{r}
'{"foo": 4, "bar": 7}' %>% select(.foo == 4)
```
More complicated. Combine more than one condition; combine each individual
filtering task in parentheses
```{r}
x <- '{"foo": 4, "bar": 2} {"foo": 5, "bar": 4} {"foo": 8, "bar": 12}'
x %>% select((.foo < 6) && (.bar > 3))
x %>% select((.foo < 6) || (.bar > 3))
```
#### types
get type information for each element
```{r}
'[0, false, [], {}, null, "hello"]' %>% types
'[0, false, [], {}, null, "hello", true, [1,2,3]]' %>% types
```
select elements by type
```{r}
'[0, false, [], {}, null, "hello"]' %>% index() %>% type(booleans)
```
#### key operations
get keys
```{r}
str <- '{"foo": 5, "bar": 7}'
str %>% keys()
```
delete by key name
```{r}
str %>% del(bar)
```
check for key existence
```{r}
str3 <- '[[0,1], ["a","b","c"]]'
str3 %>% haskey(2)
str3 %>% haskey(1,2)
```
Build an object, selecting variables by name, and rename
```{r}
'{"foo": 5, "bar": 7}' %>% build_object(a = .foo)
```
More complicated `build_object()`, using the included dataset `commits`
```{r}
commits %>%
index() %>%
build_object(sha = .sha, name = .commit.committer.name)
```
#### Maths
```{r}
'{"a": 7}' %>% do(.a + 1)
'{"a": [1,2], "b": [3,4]}' %>% do(.a + .b)
'{"a": [1,2], "b": [3,4]}' %>% do(.a - .b)
'{"a": 3}' %>% do(4 - .a)
'["xml", "yaml", "json"]' %>% do('. - ["xml", "yaml"]')
'5' %>% do(10 / . * 3)
```
comparisons
```{r}
'[5,4,2,7]' %>% index() %>% do(. < 4)
'[5,4,2,7]' %>% index() %>% do(. > 4)
'[5,4,2,7]' %>% index() %>% do(. <= 4)
'[5,4,2,7]' %>% index() %>% do(. >= 4)
'[5,4,2,7]' %>% index() %>% do(. == 4)
'[5,4,2,7]' %>% index() %>% do(. != 4)
```
length
```{r}
'[[1,2], "string", {"a":2}, null]' %>% index %>% lengthj
```
sqrt
```{r}
'9' %>% sqrtj
```
floor
```{r}
'3.14159' %>% floorj
```
find minimum
```{r}
'[5,4,2,7]' %>% minj
'[{"foo":1, "bar":14}, {"foo":2, "bar":3}]' %>% minj
'[{"foo":1, "bar":14}, {"foo":2, "bar":3}]' %>% minj(foo)
'[{"foo":1, "bar":14}, {"foo":2, "bar":3}]' %>% minj(bar)
```
find maximum
```{r}
'[5,4,2,7]' %>% maxj
'[{"foo":1, "bar":14}, {"foo":2, "bar":3}]' %>% maxj
'[{"foo":1, "bar":14}, {"foo":2, "bar":3}]' %>% maxj(foo)
'[{"foo":1, "bar":14}, {"foo":2, "bar":3}]' %>% maxj(bar)
```
#### Combine into valid JSON
`jq` sometimes creates pieces of JSON that are valid in themselves, but together are not.
`combine()` is a way to make valid JSON.
This outputs a few pieces of JSON
```{r}
(x <- commits %>%
index() %>%
build_object(sha = .sha, name = .commit.committer.name))
```
Use `combine()` to put them together.
```{r}
combine(x)
```
## Meta
* Please [report any issues or bugs](https://github.com/ropensci/jqr/issues).
* License: MIT
* Get citation information for `jqr` in R doing `citation(package = 'jqr')`
* Please note that this package is released with a [Contributor Code of Conduct](https://ropensci.org/code-of-conduct/). By contributing to this project, you agree to abide by its terms.
[![rofooter](https://www.ropensci.org/public_images/github_footer.png)](https://ropensci.org)