knitr::opts_chunk$set(
    comment = "#>",
    collapse = TRUE,
    warning = FALSE,
    message = FALSE
)

Introduction to jqr

jq is a lightweight and flexible command-line JSON processor, written in C. It's super fast, and very flexible. jq gives you the ability to index into, parse, and do calculations on JSON data. You can cut up and filter JSON data. You can change JSON key names and values. jq lets you do conditionals and comparisons, and write your own custom functions to operate on JSON data.

You can convert JSON into an R list or other R data structure, and proceed with data parsing, but why not do your JSON parsing on the actual JSON if it's easy enough? That's where jq comes in. Doing your data manipulations on the actual JSON makes it easy to pass data to downstream processes that expect JSON.

If you already familiar with jq by using it on the command line you can use the exact same commands with jqr. If you've never used jq, jqr makes jq easy to learn with a domain specific language - and you can learn the actual jq syntax as you go and apply it on the command line outside of R.

NSE vs. SE

Many functions in jqr have NSE (non-standard evaluation) as well as SE (standard evaluation) versions, where the NSE version for sorting an array is sortj() whereas the SE version is sortj_(). Some functions only have one version, and behave under SE rules.

When you pass JSON into a function as the first parameter (like ad('["a","b","c"]')) rather than piping it in (like '["a","b","c"]' %>% ad), jq() is not executed. Rather you get back an object of class jqr that holds the data you passed in and the query. To execute the query on the data, run jq(), e.g., like jq(ad('["a","b","c"]')) or ad('["a","b","c"]') %>% jq().

When piping JSON to DSL functions jq() is executed on the last DSL function used.

jqr API

There's low and high level (or DSL [domain specific language]) interfaces in jqr.

jqr low level interface

The low level and high level interfaces are unified via the function jq(). You can access the low leve interface by using jq() directly, passing a JSON string as the first parameter, the program (query) as the second, and the flags as the third (by default no flags are passed).

For example, a JSON string could be '{"a": 7, "b": 4}', and the program could be ., resulting in

{
    "a": 7,
    "b": 4
}

The program passed is exactly the same as you'd pass on the command line. Because this is a simple replication of the command line in R, there is a higher level interface, or DSL, to make it easier to use jq. Nonetheless, the low level interface is important as some jq veterans may not want to deal with a DSL, and you may need to drop down to the low level interface if the DSL doesn't work for some reason.

jqr DSL

The jqr DSL uses a suite of functions to construct queries that are executed internally with jq() after the last piped command. We use some logic to determine whether the function call is the last in a series of pipes, and if so, we run jq() on the JSON string and program/query passed.

You don't have to use pipes - they are optional. Though they do make things easier in that you can build up queries easily, just as you would with jq, or any other tools, on the command line.

Load jqr

library("jqr")

Utility functions

Peek

'{"a": 7}' %>% do(.a + 1) %>% peek
'[8,3,null,6]' %>% sortj %>% peek

String

'{"a": 7}' %>% do(.a + 1) %>% string
'[8,3,null,6]' %>% sortj %>% string

Combine

x <- '{"foo": 5, "bar": 7}' %>% select(a = .foo)
combine(x)

index

x <- '[{"message": "hello", "name": "jenn"}, {"message": "world", "name": "beth"}]'
x %>% index()

sort

Note the function name is sortj to avoid collision with base::sort. In addition, a number of other functions in this package that conflict with base R functions have a j on the end.

'[8,3,null,6]' %>% sortj

sort in reverse order

'[1,2,3,4]' %>% reverse

join

'["a","b,c,d","e"]' %>% join
'["a","b,c,d","e"]' %>% join(`;`)

starts- and ends-with

'["fo", "foo", "barfoo", "foobar", "barfoob"]' %>% index %>% endswith(foo)
'["fo", "foo", "barfoo", "foobar", "barfoob"]' %>% index %>% startswith(foo)

contains

'"foobar"' %>% contains("bar")

unique

'[1,2,5,3,5,3,1,3]' %>% uniquej

data types

Get type information for each element

'[0, false, [], {}, null, "hello"]' %>% types
'[0, false, [], {}, null, "hello", true, [1,2,3]]' %>% types

Select elements by type

'[0, false, [], {}, null, "hello"]' %>% index() %>% type(booleans)

keys

Get keys

str <- '{"foo": 5, "bar": 7}'
str %>% keys()

Delete by key name

str %>% del(bar)
str %>% del(foo)

Check for key existence

str3 <- '[[0,1], ["a","b","c"]]'
str3 %>% haskey(2)
str3 %>% haskey(1,2)

select

Select variables by name, and rename

'{"foo": 5, "bar": 7}' %>% select(a = .foo)

More complicated select(), using the included dataset commits

commits %>%
  index() %>%
  build_object(sha = .sha, name = .commit.committer.name)

maths

Maths comparisons

'[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)

sqrt

'9' %>% sqrtj

floor

'3.14159' %>% floorj

find minimum

'[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

'[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)

connections

files

tmp <- tempfile()
writeLines(c("[123, 456]", "[77, 88, 99]", "[41]"), tmp)
jq(file(tmp), ".[]")

urls

x <- 'http://jeroen.github.io/data/diamonds.json'
jq(url(x), "select(.carat > 3.5)")


ropensci/jqr documentation built on Jan. 19, 2024, 8:33 p.m.