jqr: Client for 'jq', a JSON Processor

JSONパーサー

> library(jqr)

Attaching package: 'jqr'
The following object is masked from 'package:dendextend':

    %>%

バージョン: 0.2.0


関数名 概略
at Format strings and escaping
combine Combine json pieces
dot dot and related functions
funs Define and use functions
githubcommits GitHub Commits Data
index index and related functions
jq Execute a query with jq
jq_flags Flags for use with jq
jqr jqr: An R client for the C library jq
keys Operations on keys, or by keys
logicaltests Logical tests
manip Manipulation operations
maths Math operations
paths Outputs paths to all the elements in its input
peek Peek at a query
rangej Produce range of numbers
recurse Search through a recursive structure - extract data from all levels
select Select variables
sortj Sort and related
string Give back a character string
types Types and related functions
vars Variables

at

> z <- '[1, 2, 3, "a"]'
> z %>% at(csv) # csv形式で出力
"1,2,3,\"a\""
> z %>% index() %>% at(text)
[
    "1",
    "2",
    "3",
    "a"
]

combine

> x <- '{"foo": 5, "bar": 7}' %>% select(a = .foo)
> combine(x)
{
    "a": 5
}
> x <- '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}'
> # x %>% select(user, title = `.titles[]`) %>% combine() %>% jsonlite::validate()

githubcommits

> githubcommits %>% index() %>% select(sha = .sha)
[
    {
        "sha": [
            "110e009996e1359d25b8e99e71f83b96e5870790"
        ]
    },
    {
        "sha": [
            "7b6a018dff623a4f13f6bcd52c7c56d9b4a4165f"
        ]
    },
    {
        "sha": [
            "a50e548cc5313c187483bc8fb1b95e1798e8ef65"
        ]
    },
    {
        "sha": [
            "4b258f7d31b34ff5d45fba431169e7fd4c995283"
        ]
    },
    {
        "sha": [
            "d1cb8ee0ad3ddf03a37394bfa899cfd3ddd007c5"
        ]
    }
]

index / indexif / dotindex

> x <- '[{"message": "hello", "name": "jenn"}, {"message": "world", "name": "beth"}]'
> x %>% index()
[
    {
        "message": "hello",
        "name": "jenn"
    },
    {
        "message": "world",
        "name": "beth"
    }
]
> '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' %>% dotindex(name)
[
    "JSON",
    "XML"
]
> '[{"name":"JSON", "good":{"foo":5}}, {"name":"XML", "good":{"foo":6}}]' %>% dotindex(good.foo)
[
    5,
    6
]

jq_flags / flags

Arguments

  • pretty
  • ascii
  • color
  • sorted
  • .data
> '{"a": 7, "z":0, "b": 4}' %>% flags(sorted = TRUE)
{
    "a": 7,
    "b": 4,
    "z": 0
}
> '{"a": 7, "z":0, "b": 4}' %>% dot() %>% flags(sorted = FALSE)
{
    "a": 7,
    "z": 0,
    "b": 4
}
> jq('{"a": 7, "z":0, "b": 4}', ".", flags = jq_flags(sorted = TRUE))
{
    "a": 7,
    "b": 4,
    "z": 0
}

keys / del / haskey

> '{"foo": 5, "bar": 7}' %>% keys()
[
    "bar",
    "foo"
]
> '[[0,1], ["a","b","c"]]' %>% haskey(2)
[
    false,
    true
]

manip / join / splitj / ltrimstr / rtrimstr / startswith / endswith / index_loc / rindex_loc / indices / tojson / fromjson / tostring / tonumber / contains / uniquej / group

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

math / do / lengthj / sqrtj / floorj / minj / maxj / ad / map

> '[5,4,2,7]' %>% index() %>% do(. > 4)
[
    true,
    false,
    false,
    true
]
> '9' %>% sqrtj()
3
> '[{"foo":1, "bar":14}, {"foo":2, "bar":3}]' %>% minj(bar)
{
    "foo": 2,
    "bar": 3
}

peek

> '{"a": 7}' %>% do(.a + 1) %>% peek()
<jq query>
  query: .a + 1

rangej

> rangej(2:4)
$data
[1] "null"

$args
$args[[1]]
[1] "range(2;4)"
attr(,"type")
[1] "range"


attr(,"class")
[1] "jqr"
> 2:4 %>% rangej
[
    2,
    3
]

string

> '[8,3,null,6]' %>% sortj() %>% string()
[1] "[8,3,null,6]"
> z <- '[1, 2, 3, "a"]'
> z %>% index() %>% at(text) %>% 
+   string() %>% 
+   jsonlite::fromJSON(txt = .)
[1] "1" "2" "3" "a"

sortj / reverse

並び替え

> '[8,3,null,6]' %>% sortj()
[
    null,
    3,
    6,
    8
]
> '[1,2,3,4]' %>% reverse
[
    4,
    3,
    2,
    1
]

types

> '[0, false, [], {}, null, "hello", true, [1,2,3]]' %>% types()
[
    "number",
    "boolean",
    "array",
    "object",
    "null",
    "string",
    "boolean",
    "array"
]